Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse self-nested generic struct #1420

Merged
merged 2 commits into from Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions generics.go
Expand Up @@ -95,13 +95,14 @@ func (pkgDefs *PackagesDefinitions) parametrizeGenericType(file *ast.File, origi
NamePos: original.TypeSpec.Name.NamePos,
Obj: original.TypeSpec.Name.Obj,
},
Type: pkgDefs.resolveGenericType(original.File, original.TypeSpec.Type, genericParamTypeDefs),
Doc: original.TypeSpec.Doc,
Assign: original.TypeSpec.Assign,
},
}
pkgDefs.uniqueDefinitions[name] = parametrizedTypeSpec

parametrizedTypeSpec.TypeSpec.Type = pkgDefs.resolveGenericType(original.File, original.TypeSpec.Type, genericParamTypeDefs)

return parametrizedTypeSpec
}

Expand Down Expand Up @@ -184,7 +185,7 @@ func (pkgDefs *PackagesDefinitions) resolveGenericType(file *ast.File, expr ast.
fullGenericName, _ := getGenericFieldType(file, expr, genericParamTypeDefs)
typeDef := pkgDefs.FindTypeSpec(fullGenericName, file)
if typeDef != nil {
return typeDef.TypeSpec.Type
return typeDef.TypeSpec.Name
}
case *ast.StructType:
newStructTypeDef := &ast.StructType{
Expand Down
2 changes: 2 additions & 0 deletions generics_test.go
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"go/ast"
"io/fs"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -84,6 +85,7 @@ func TestParseGenericsProperty(t *testing.T) {
err = p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)
b, err := json.MarshalIndent(p.swagger, "", " ")
os.WriteFile(searchDir+"/expected.json", b, fs.ModePerm)
assert.NoError(t, err)
assert.Equal(t, string(expected), string(b))
}
Expand Down
10 changes: 10 additions & 0 deletions testdata/generics_nested/api/api.go
Expand Up @@ -38,3 +38,13 @@ func GetPosts(w http.ResponseWriter, r *http.Request) {
func GetPostArray(w http.ResponseWriter, r *http.Request) {
_ = web.GenericNestedResponse[types.Post]{}
}

// @Summary List Posts
// @Description Get All of the Posts
// @Accept json
// @Produce json
// @Success 200 {object} web.GenericNodeThree[string]
// @Router /posts-self-nested-struct/ [get]
func GetPostSelfNestStruct(w http.ResponseWriter, r *http.Request) {

}
37 changes: 37 additions & 0 deletions testdata/generics_nested/expected.json
Expand Up @@ -130,6 +130,26 @@
}
}
}
},
"/posts-self-nested-struct/": {
"get": {
"description": "Get All of the Posts",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "List Posts",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/web.GenericNodeThree-string"
}
}
}
}
}
},
"definitions": {
Expand Down Expand Up @@ -580,6 +600,23 @@
"type": "string"
}
}
},
"web.GenericNodeThree-string": {
"type": "object",
"properties": {
"current": {
"type": "array",
"items": {
"type": "string"
}
},
"next": {
"type": "array",
"items": {
"$ref": "#/definitions/web.GenericNodeThree-string"
}
}
}
}
}
}
5 changes: 5 additions & 0 deletions testdata/generics_nested/web/handler.go
Expand Up @@ -62,3 +62,8 @@ type APIError struct {
ErrorCtx string // Error `context` tick comment
CreatedAt time.Time // Error time
}

type GenericNodeThree[T any] struct {
CurrentData []T `json:"current"`
Next []*GenericNodeThree[T] `json:"next"`
}
115 changes: 61 additions & 54 deletions testdata/generics_property/expected.json
Expand Up @@ -184,15 +184,7 @@
}
},
"value4": {
"type": "object",
"properties": {
"subValue1": {
"$ref": "#/definitions/api.Person"
},
"subValue2": {
"type": "string"
}
}
"$ref": "#/definitions/types.SubField1-api_Person-string"
}
}
},
Expand Down Expand Up @@ -221,15 +213,7 @@
}
},
"value4": {
"type": "object",
"properties": {
"subValue1": {
"$ref": "#/definitions/api.Person"
},
"subValue2": {
"type": "string"
}
}
"$ref": "#/definitions/types.SubField1-api_Person-string"
}
}
},
Expand Down Expand Up @@ -258,15 +242,7 @@
}
},
"value4": {
"type": "object",
"properties": {
"subValue1": {
"$ref": "#/definitions/types.Post"
},
"subValue2": {
"type": "string"
}
}
"$ref": "#/definitions/types.SubField1-types_Post-string"
}
}
},
Expand All @@ -286,15 +262,7 @@
}
},
"value4": {
"type": "object",
"properties": {
"subValue1": {
"type": "string"
},
"subValue2": {
"type": "string"
}
}
"$ref": "#/definitions/types.SubField1-string-string"
}
}
},
Expand All @@ -314,15 +282,7 @@
}
},
"value4": {
"type": "object",
"properties": {
"subValue1": {
"$ref": "#/definitions/types.Field-api_Person"
},
"subValue2": {
"type": "string"
}
}
"$ref": "#/definitions/types.SubField1-types_Field-api_Person-string"
}
}
},
Expand All @@ -342,15 +302,7 @@
}
},
"value4": {
"type": "object",
"properties": {
"subValue1": {
"$ref": "#/definitions/types.Field-string"
},
"subValue2": {
"type": "string"
}
}
"$ref": "#/definitions/types.SubField1-types_Field-string-string"
}
}
},
Expand Down Expand Up @@ -385,6 +337,61 @@
}
}
},
"types.SubField1-api_Person-string": {
"type": "object",
"properties": {
"subValue1": {
"$ref": "#/definitions/api.Person"
},
"subValue2": {
"type": "string"
}
}
},
"types.SubField1-string-string": {
"type": "object",
"properties": {
"subValue1": {
"type": "string"
},
"subValue2": {
"type": "string"
}
}
},
"types.SubField1-types_Field-api_Person-string": {
"type": "object",
"properties": {
"subValue1": {
"$ref": "#/definitions/types.Field-api_Person"
},
"subValue2": {
"type": "string"
}
}
},
"types.SubField1-types_Field-string-string": {
"type": "object",
"properties": {
"subValue1": {
"$ref": "#/definitions/types.Field-string"
},
"subValue2": {
"type": "string"
}
}
},
"types.SubField1-types_Post-string": {
"type": "object",
"properties": {
"subValue1": {
"$ref": "#/definitions/types.Post"
},
"subValue2": {
"type": "string"
}
}
},
"web.PostResponse": {
"type": "object",
"properties": {
Expand Down