Skip to content

Commit

Permalink
Fix isNotRecurringNestStruct function
Browse files Browse the repository at this point in the history
Currently, the function does not use its refTypeName parameter;
instead it checks that the 1st structure in the structures stack
does not repeat. Recursion, however, can be indirect. Consider the
following example:

    type Foo struct {
        Bar Bar
    }

    type Bar struct {
        Foo Foo
    }

    type Baz struct {
        Foo Foo
    }

In the example generator will crash with stack overflow while trying to
generate spec for Baz when there is no spec for Foo and Bar generated
yet.

The fix is to check that refTypeName does not repeat in the structures
stack.
  • Loading branch information
Nikolai Obedin committed Nov 7, 2018
1 parent cea5516 commit fd49f74
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
8 changes: 2 additions & 6 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,8 @@ var structStacks []string

// isNotRecurringNestStruct check if a structure that is not a not repeating
func isNotRecurringNestStruct(refTypeName string, structStacks []string) bool {
if len(structStacks) <= 0 {
return true
}
startStruct := structStacks[0]
for _, v := range structStacks[1:] {
if startStruct == v {
for _, v := range structStacks {
if refTypeName == v {
return false
}
}
Expand Down
11 changes: 11 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,17 @@ func TestParseSimpleApi(t *testing.T) {
}
}
},
"web.IndirectRecursiveTest": {
"type": "object",
"properties": {
"Tags": {
"type": "array",
"items": {
"$ref": "#/definitions/web.Tag"
}
}
}
},
"web.Pet": {
"type": "object",
"required": [
Expand Down
5 changes: 5 additions & 0 deletions testdata/simple/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ func Pet2() {

}

// @Summary Use IndirectRecursiveTest
// @Success 200 {object} web.IndirectRecursiveTest
func IndirectRecursiveTest() {
}

type Pet3 struct {
ID int `json:"id"`
}
4 changes: 4 additions & 0 deletions testdata/simple/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ type Pet2 struct {
DeletedAt *time.Time `json:"deleted_at"`
}

type IndirectRecursiveTest struct {
Tags []Tag
}

type APIError struct {
ErrorCode int
ErrorMessage string
Expand Down

0 comments on commit fd49f74

Please sign in to comment.