Skip to content

Fixes list recursion #2022

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

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion field_parser_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func TestDefaultFieldParserV3(t *testing.T) {
t.Parallel()

schema := spec.NewSchemaSpec()
schema.Spec.Type = []string{"string"}
schema.Spec.Type = &spec.SingleOrArray[string]{"string"}
parser := &Parser{}
fieldParser := newTagBaseFieldParserV3(
parser,
Expand Down
7 changes: 6 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1395,8 +1395,13 @@ func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef) (*Schema, error)
if parser.isInStructStack(typeSpecDef) {
parser.debug.Printf("Skipping '%s', recursion detected.", typeName)

schemaName := typeName
if typeSpecDef.SchemaName != "" {
schemaName = typeSpecDef.SchemaName
}

return &Schema{
Name: typeName,
Name: schemaName,
PkgPath: typeSpecDef.PkgPath,
Schema: PrimitiveSchema(OBJECT),
},
Expand Down
33 changes: 26 additions & 7 deletions parserv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,12 +702,25 @@ func (p *Parser) ParseDefinitionV3(typeSpecDef *TypeSpecDef) (*SchemaV3, error)
if p.isInStructStack(typeSpecDef) {
p.debug.Printf("Skipping '%s', recursion detected.", typeName)

return &SchemaV3{
Name: typeName,
PkgPath: typeSpecDef.PkgPath,
Schema: PrimitiveSchemaV3(OBJECT).Spec,
},
ErrRecursiveParseStruct
schemaName := typeName
if typeSpecDef.SchemaName != "" {
schemaName = typeSpecDef.SchemaName
}

schema := &SchemaV3{
Name: schemaName,
PkgPath: typeSpecDef.PkgPath,
Schema: PrimitiveSchemaV3(OBJECT).Spec,
}

p.parsedSchemasV3[typeSpecDef] = schema

if p.openAPI.Components.Spec.Schemas == nil {
p.openAPI.Components.Spec.Schemas = make(map[string]*spec.RefOrSpec[spec.Schema])
}
p.openAPI.Components.Spec.Schemas[schema.Name] = spec.NewRefOrSpec(nil, schema.Schema)

return schema, ErrRecursiveParseStruct
}

p.structStack = append(p.structStack, typeSpecDef)
Expand Down Expand Up @@ -1086,5 +1099,11 @@ func (p *Parser) GetSchemaTypePathV3(schema *spec.RefOrSpec[spec.Schema], depth

func (p *Parser) getSchemaByRef(ref *spec.Ref) *spec.Schema {
searchString := strings.ReplaceAll(ref.Ref, "#/components/schemas/", "")
return p.openAPI.Components.Spec.Schemas[searchString].Spec
schemaRef, exists := p.openAPI.Components.Spec.Schemas[searchString]
if !exists || schemaRef == nil {
println(fmt.Sprintf("Schema not found for ref: %s, returning any", ref.Ref))
return &spec.Schema{} // return empty schema if not found
}

return schemaRef.Spec
}
56 changes: 56 additions & 0 deletions parserv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,59 @@ func TestParseTypeAlias(t *testing.T) {

assert.JSONEq(t, string(expected), string(result))
}

func TestParseRecursionWithSchemaName(t *testing.T) {
t.Parallel()

searchDir := "testdata/recursion_schema_name"
p := New(GenerateOpenAPI3Doc(true))

err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
require.NoError(t, err)

userSchema, exists := p.openAPI.Components.Spec.Schemas["User"]
require.True(t, exists, "User schema should exist")
require.NotNil(t, userSchema, "User schema should not be nil")
require.NotNil(t, userSchema.Spec, "User schema spec should not be nil")

assert.Equal(t, "object", (*userSchema.Spec.Type)[0])

childrenProp, exists := userSchema.Spec.Properties["children"]
require.True(t, exists, "children property should exist")
require.NotNil(t, childrenProp.Spec, "children property spec should not be nil")

assert.Equal(t, "array", (*childrenProp.Spec.Type)[0])

require.NotNil(t, childrenProp.Spec.Items, "children items should not be nil")
require.NotNil(t, childrenProp.Spec.Items.Schema, "children items schema should not be nil")

expectedRef := "#/components/schemas/User"
assert.Equal(t, expectedRef, childrenProp.Spec.Items.Schema.Ref.Ref)
}

func TestGetSchemaByRef(t *testing.T) {
t.Parallel()

p := New(GenerateOpenAPI3Doc(true))
p.openAPI.Components.Spec.Schemas = make(map[string]*spec.RefOrSpec[spec.Schema])

t.Run("Existing schema", func(t *testing.T) {
testSchema := &spec.Schema{}
testSchema.Type = &spec.SingleOrArray[string]{"string"}
p.openAPI.Components.Spec.Schemas["TestSchema"] = spec.NewRefOrSpec(nil, testSchema)

ref := &spec.Ref{Ref: "#/components/schemas/TestSchema"}
result := p.getSchemaByRef(ref)

require.NotNil(t, result)
assert.Equal(t, testSchema, result)
})

t.Run("Non-existing schema returns empty schema", func(t *testing.T) {
ref := &spec.Ref{Ref: "#/components/schemas/NonExistentSchema"}
result := p.getSchemaByRef(ref)

require.NotNil(t, result)
assert.Equal(t, &spec.Schema{}, result)
})
}
24 changes: 24 additions & 0 deletions testdata/recursion_schema_name/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

// User represents a user with self-references
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Children []*User `json:"children,omitempty"`
} // @name User

// @title Test API
// @version 1.0
// @description Test API for recursion with schema name
// @BasePath /
func main() {}

// GetUser returns a user
// @Summary Get user
// @Description Get user by ID
// @Tags users
// @Accept json
// @Produce json
// @Success 200 {object} User
// @Router /user [get]
func GetUser() {}