Skip to content

Commit

Permalink
Merge cf009bd into b8f53da
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianMartin authored Sep 15, 2022
2 parents b8f53da + cf009bd commit 2eb60a0
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 14 deletions.
39 changes: 25 additions & 14 deletions packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,25 +389,17 @@ func (pkgDefs *PackagesDefinitions) FindTypeSpec(typeName string, file *ast.File
}
}

if strings.Contains(typeName, "[") {
// joinedParts differs from typeName in that it does not contain any type parameters
joinedParts := strings.Join(parts, ".")
for tName, tSpec := range pkgDefs.uniqueDefinitions {
if !strings.Contains(tName, "[") {
continue
}

if strings.Contains(tName, joinedParts) {
if parametrized := pkgDefs.parametrizeStruct(file, tSpec, typeName, parseDependency); parametrized != nil {
return parametrized
}
}
}
if def := pkgDefs.findGenericTypeSpec(typeName, file, parseDependency); def != nil {
return def
}

return pkgDefs.findTypeSpec(pkgPath, parts[1])
}

if def := pkgDefs.findGenericTypeSpec(fullTypeName(file.Name.Name, typeName), file, parseDependency); def != nil {
return def
}

typeDef, ok := pkgDefs.uniqueDefinitions[fullTypeName(file.Name.Name, typeName)]
if ok {
return typeDef
Expand All @@ -429,3 +421,22 @@ func (pkgDefs *PackagesDefinitions) FindTypeSpec(typeName string, file *ast.File

return nil
}

func (pkgDefs *PackagesDefinitions) findGenericTypeSpec(typeName string, file *ast.File, parseDependency bool) *TypeSpecDef {
if strings.Contains(typeName, "[") {
// genericName differs from typeName in that it does not contain any type parameters
genericName := strings.SplitN(typeName, "[", 2)[0]
for tName, tSpec := range pkgDefs.uniqueDefinitions {
if !strings.Contains(tName, "[") {
continue
}

if strings.Contains(tName, genericName) {
if parametrized := pkgDefs.parametrizeStruct(file, tSpec, typeName, parseDependency); parametrized != nil {
return parametrized
}
}
}
}
return nil
}
13 changes: 13 additions & 0 deletions testdata/generics_basic/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ import (
"github.com/swaggo/swag/testdata/generics_basic/web"
)

type Response[T any, X any] struct {
Data T
Meta X

Status string
}

type StringStruct struct {
Data string
}

// @Summary Add a new pet to the store
// @Description get string by ID
// @Accept json
Expand All @@ -16,6 +27,8 @@ import (
// @Success 201 {object} web.GenericResponse[types.Hello]
// @Success 202 {object} web.GenericResponse[types.Field[string]]
// @Success 203 {object} web.GenericResponse[types.Field[int]]
// @Success 204 {object} Response[string, types.Field[int]]
// @Success 205 {object} Response[StringStruct, types.Field[int]]
// @Success 222 {object} web.GenericResponseMulti[types.Post, types.Post]
// @Failure 400 {object} web.APIError "We need ID!!"
// @Failure 404 {object} web.APIError "Can not find ID"
Expand Down
48 changes: 48 additions & 0 deletions testdata/generics_basic/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@
"$ref": "#/definitions/web.GenericResponse-types_Field_int"
}
},
"204": {
"description": "No Content",
"schema": {
"$ref": "#/definitions/api.Response-string-types_Field_int"
}
},
"205": {
"description": "Reset Content",
"schema": {
"$ref": "#/definitions/api.Response-api_StringStruct-types_Field_int"
}
},
"222": {
"description": "",
"schema": {
Expand All @@ -170,6 +182,42 @@
}
},
"definitions": {
"api.Response-api_StringStruct-types_Field_int": {
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/api.StringStruct"
},
"meta": {
"$ref": "#/definitions/types.Field-int"
},
"status": {
"type": "string"
}
}
},
"api.Response-string-types_Field_int": {
"type": "object",
"properties": {
"data": {
"type": "string"
},
"meta": {
"$ref": "#/definitions/types.Field-int"
},
"status": {
"type": "string"
}
}
},
"api.StringStruct": {
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
},
"types.Field-int": {
"type": "object",
"properties": {
Expand Down

0 comments on commit 2eb60a0

Please sign in to comment.