Skip to content

Commit

Permalink
adds support for pointer function scoped fields (#1841)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferFJ committed Jul 11, 2024
1 parent ff50cd6 commit c7f1cd8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
19 changes: 11 additions & 8 deletions packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,21 @@ func (pkgDefs *PackagesDefinitions) parseFunctionScopedTypesFromFile(astFile *as
fullName := typeSpecDef.TypeName()
if structType, ok := typeSpecDef.TypeSpec.Type.(*ast.StructType); ok {
for _, field := range structType.Fields.List {
if idt, ok := field.Type.(*ast.Ident); ok && !IsGolangPrimitiveType(idt.Name) {
var idt *ast.Ident
var ok bool
switch field.Type.(type) {
case *ast.Ident:
idt, ok = field.Type.(*ast.Ident)
case *ast.StarExpr:
idt, ok = field.Type.(*ast.StarExpr).X.(*ast.Ident)
case *ast.ArrayType:
idt, ok = field.Type.(*ast.ArrayType).Elt.(*ast.Ident)
}
if ok && !IsGolangPrimitiveType(idt.Name) {
if functype, ok := functionScopedTypes[idt.Name]; ok {
idt.Name = functype.TypeName()
}
}
if art, ok := field.Type.(*ast.ArrayType); ok {
if idt, ok := art.Elt.(*ast.Ident); ok && !IsGolangPrimitiveType(idt.Name) {
if functype, ok := functionScopedTypes[idt.Name]; ok {
idt.Name = functype.TypeName()
}
}
}
}
}

Expand Down
16 changes: 15 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3407,8 +3407,18 @@ func Fun() {
Name string
}
type pointerChild struct {
Name string
}
type arrayChild struct {
Name string
}
type child struct {
GrandChild grandChild
GrandChild grandChild
PointerChild *pointerChild
ArrayChildren []arrayChild
}
type response struct {
Expand All @@ -3430,6 +3440,10 @@ func Fun() {
assert.True(t, ok)
_, ok = p.swagger.Definitions["main.Fun.grandChild"]
assert.True(t, ok)
_, ok = p.swagger.Definitions["main.Fun.pointerChild"]
assert.True(t, ok)
_, ok = p.swagger.Definitions["main.Fun.arrayChild"]
assert.True(t, ok)
}

func TestParseFunctionScopedStructRequestResponseJSON(t *testing.T) {
Expand Down
7 changes: 6 additions & 1 deletion testdata/simple/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ func GetPet6FunctionScopedComplexResponse() {
Name string
}

type pointerPet struct {
Name string
}

type response struct {
Pets []pet
Pets []pet
PointerPet *pointerPet
}
}
11 changes: 11 additions & 0 deletions testdata/simple/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,14 @@
}
}
},
"api.GetPet6FunctionScopedComplexResponse.pointerPet": {
"type": "object",
"properties": {
"Name": {
"type": "string"
}
}
},
"api.GetPet6FunctionScopedComplexResponse.response": {
"type": "object",
"properties": {
Expand All @@ -429,6 +437,9 @@
"items": {
"$ref": "#/definitions/api.GetPet6FunctionScopedComplexResponse.pet"
}
},
"PointerPet": {
"$ref": "#/definitions/api.GetPet6FunctionScopedComplexResponse.pointerPet"
}
}
},
Expand Down

0 comments on commit c7f1cd8

Please sign in to comment.