Skip to content

Commit

Permalink
fix: fixed definition parsing for alias types (#1688)
Browse files Browse the repository at this point in the history
  • Loading branch information
artem328 authored Nov 7, 2023
1 parent 86df652 commit 37dac67
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 1 deletion.
2 changes: 1 addition & 1 deletion parserv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ func (p *Parser) parseTypeExprV3(file *ast.File, typeExpr ast.Expr, ref bool) (*

// type Foo Baz
case *ast.Ident:
result, err := p.getTypeSchemaV3(expr.Name, file, true)
result, err := p.getTypeSchemaV3(expr.Name, file, ref)
if err != nil {
return nil, errors.Wrap(err, errMessage)
}
Expand Down
21 changes: 21 additions & 0 deletions parserv3_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package swag

import (
"encoding/json"
"go/ast"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -398,3 +400,22 @@ func TestParserParseServers(t *testing.T) {
assert.Equal(t, "Production Petstore server.", servers[1].Spec.Description)

}

func TestParseTypeAlias(t *testing.T) {
t.Parallel()

searchDir := "testdata/v3/type_alias_definition"

p := New(GenerateOpenAPI3Doc(true))

err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
require.NoError(t, err)

expected, err := os.ReadFile(filepath.Join(searchDir, "expected.json"))
require.NoError(t, err)

result, err := json.Marshal(p.openAPI)
require.NoError(t, err)

assert.JSONEq(t, string(expected), string(result))
}
10 changes: 10 additions & 0 deletions testdata/v3/type_alias_definition/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package api

import "net/http"

// @Router /test [GET]
// @Produce json
// @Success 200 {object} types.Response "Success"
func Handle(w http.ResponseWriter, r *http.Request) {

}
110 changes: 110 additions & 0 deletions testdata/v3/type_alias_definition/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"openapi": "3.1.0",
"info": {
"title": "Swagger Example API",
"version": "1.0"
},
"externalDocs": {
"description": "",
"url": ""
},
"components": {
"schemas": {
"types.NestedStruct": {
"type": "object",
"properties": {
"int": {
"type": "integer"
}
}
},
"types.Struct": {
"type": "object",
"properties": {
"string": {
"type": "string"
},
"nestedStruct": {
"$ref": "#/components/schemas/types.NestedStruct"
}
}
},
"types.StructAlias": {
"type": "object",
"properties": {
"string": {
"type": "string"
},
"nestedStruct": {
"$ref": "#/components/schemas/types.NestedStruct"
}
}
},
"types.StructSubtype": {
"type": "object",
"properties": {
"string": {
"type": "string"
},
"nestedStruct": {
"$ref": "#/components/schemas/types.NestedStruct"
}
}
},
"types.OtherStructAlias": {
"type": "object",
"properties": {
"float": {
"type": "number"
}
}
},
"types.OtherStructSubtype": {
"type": "object",
"properties": {
"float": {
"type": "number"
}
}
},
"types.Response": {
"type": "object",
"properties": {
"struct": {
"$ref": "#/components/schemas/types.Struct"
},
"structAlias": {
"$ref": "#/components/schemas/types.StructAlias"
},
"structSubtype": {
"$ref": "#/components/schemas/types.StructSubtype"
},
"otherStructAlias": {
"$ref": "#/components/schemas/types.OtherStructAlias"
},
"otherStructSubtype": {
"$ref": "#/components/schemas/types.OtherStructSubtype"
}
}
}
}
},
"paths": {
"/test": {
"get": {
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/types.Response"
}
}
},
"description": "Success"
}
}
}
}
}
}
4 changes: 4 additions & 0 deletions testdata/v3/type_alias_definition/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

// @title Swagger Example API
// @version 1.0
5 changes: 5 additions & 0 deletions testdata/v3/type_alias_definition/othertypes/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package othertypes

type Struct struct {
Float float64
}
26 changes: 26 additions & 0 deletions testdata/v3/type_alias_definition/types/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package types

import "github.com/swaggo/swag/v2/testdata/v3/type_alias_definition/othertypes"

type Struct struct {
String string `json:"string"`
NestedStruct NestedStruct `json:"nestedStruct"`
}

type NestedStruct struct {
Int int `json:"int"`
}

type StructAlias = Struct
type OtherStructAlias = othertypes.Struct

type StructSubtype Struct
type OtherStructSubtype othertypes.Struct

type Response struct {
Struct `json:"struct"`
StructAlias `json:"structAlias"`
OtherStructAlias `json:"otherStructAlias"`
StructSubtype `json:"structSubtype"`
OtherStructSubtype `json:"otherStructSubtype"`
}

0 comments on commit 37dac67

Please sign in to comment.