Skip to content

Fixes list recursion - #2022

Merged
ubogdan merged 3 commits into
swaggo:v2from
addshore:v2-addshore-fix-list-recursion
Aug 18, 2025
Merged

ubogdan merged 3 commits into
swaggo:v2from
addshore:v2-addshore-fix-list-recursion

Conversation

@addshore

@addshore addshore commented Jun 2, 2025 •

Copy link
Copy Markdown

I had 3 issues.

  1. A panic due to nil pointer when
    p.openAPI.Components.Spec.Schemas[searchString]
    was nil, and getSchemaByRef tried to return .Spec
    Resolved with a simple check and nill return (to start)

This highlighted issue 2 which in some ways relates to #1939

  1. When an ARRAY schema type is evaluated, and getSchemaByRef returns nil, generation would fail.
    So getSchemaByRef is changed to warn and return ANY instead similar to some other places, which means the generation can work.

A few weeks passed and I fonud another issue

  1. references to names (short and long) were getting messed up during recursive stuff

I expect this could be resolved more globally at some point pulling in other things from the linked issue...

@addshore addshore changed the title Fixes list recursion by returning any Fixes list recursion Jun 18, 2025
@addshore

Copy link
Copy Markdown
Author

Just some validation of the tests I wrote.

Prior to either of my fix commits, the test fails with this:

--- FAIL: TestGetSchemaByRef (0.00s)
    --- PASS: TestGetSchemaByRef/Existing_schema (0.00s)
    --- FAIL: TestGetSchemaByRef/Non-existing_schema_returns_empty_schema (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x96bca8]

goroutine 51 [running]:
testing.tRunner.func1.2({0xa39ce0, 0xf8f160})
        /usr/local/go/src/testing/testing.go:1632 +0x230
testing.tRunner.func1()
        /usr/local/go/src/testing/testing.go:1635 +0x35e
panic({0xa39ce0?, 0xf8f160?})
        /usr/local/go/src/runtime/panic.go:785 +0x132
github.com/swaggo/swag/v2.(*Parser).getSchemaByRef(0xc000126000, 0xf?)
        /home/adam/dev/github/addshore/swaggo/swag/parserv3.go:1089 +0x68
github.com/swaggo/swag/v2.TestGetSchemaByRef.func2(0xc0004961a0)
        /home/adam/dev/github/addshore/swaggo/swag/parserv3_test.go:568 +0x57
testing.tRunner(0xc0004961a0, 0xc000498090)
        /usr/local/go/src/testing/testing.go:1690 +0xf4
created by testing.(*T).Run in goroutine 8
        /usr/local/go/src/testing/testing.go:1743 +0x390
exit status 2
FAIL    github.com/swaggo/swag/v2       0.007s

And with the first fix applied it fails with this:

=== RUN   TestGetSchemaByRef/Existing_schema
=== RUN   TestGetSchemaByRef/Non-existing_schema_returns_empty_schema
Schema not found for ref: #/components/schemas/NonExistentSchema, returning any
--- PASS: TestGetSchemaByRef (0.00s)
    --- PASS: TestGetSchemaByRef/Existing_schema (0.00s)
    --- PASS: TestGetSchemaByRef/Non-existing_schema_returns_empty_schema (0.00s)
@basepath is deprecated use servers instead
acceptAttribute is deprecated, as there is no such field on top level in spec V3.1
produce is deprecated, as there is no such field on top level in spec V3.1
2025/06/18 16:12:47 Generating main.User
2025/06/18 16:12:47 Skipping 'main.User', recursion detected.
=== NAME  TestParseRecursionWithSchemaName
    parserv3_test.go:529: 
                Error Trace:    /home/adam/dev/github/addshore/swaggo/swag/parserv3_test.go:529
                Error:          Should be true
                Test:           TestParseRecursionWithSchemaName
                Messages:       User schema should exist
--- FAIL: TestParseRecursionWithSchemaName (0.11s)
FAIL
exit status 1
FAIL    github.com/swaggo/swag/v2       0.120s

As can be seen in CI, both fixes make the tests pass

@ubogdan

ubogdan commented Aug 18, 2025

Copy link
Copy Markdown
Contributor

Please rebase and fix conflicts.

I had 2 issues.

1) A panic due to nil pointer when
p.openAPI.Components.Spec.Schemas[searchString]
was nil, and getSchemaByRef tried to return .Spec
Resolved with a simple check and nill return (to start)

This highlighted issue 2 which in some ways relates to
swaggo#1939
2) When an ARRAY schema type is evaluated, and getSchemaByRef
returns nil, generation would fail.
So getSchemaByRef is changed to warn and return ANY instead
similar to some other places, which means the generation can
work.

I expect this could be resolved more globally at some point
pulling in other things from the linked issue...
Prior to this, in cases where recursion was detected,
the annotated @name value was not used in the generated
spec.
This in turn would lead to invalid spec, as some values would
be using the full name, and others the hort name...
@addshore
addshore force-pushed the v2-addshore-fix-list-recursion branch from 1186089 to 256e493 Compare August 18, 2025 10:25

@ubogdan ubogdan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

⌂ 换网址