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

Conversation

addshore
Copy link

@addshore addshore commented Jun 2, 2025

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 added 2 commits June 2, 2025 13:30
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 changed the title Fixes list recursion by returning any Fixes list recursion Jun 18, 2025
@addshore
Copy link
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

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.

1 participant