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

adds support for pointer function scoped fields #1841

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading