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

fix: differentiate between array typres and standard structs #134

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

dylanhitt
Copy link
Collaborator

@dylanhitt dylanhitt commented Jun 26, 2024

This did turn out to be pretty complex.

The following is by no means finished. More looking for a discussion. If you take a look #133 the bug is fairly clear. We do not respect response nor return types when they are arrays.

The only way I can think of solving this is that we must create the ref to the schema and link it at the bottom of a recursion tree. If you do not do this you will end up with improper refs, with the original approach the input for the first type wins so if an array of MyStruct is passed the schema will be a list of MyStruct rather than just the simple Schema of MyStruct. See schemaDive. Currently this does require us to dive twice on request bodies. Since we seem to check request bodies if they are unknown-type/string, which I am unsure why (wouldn't we want to have []string be a valid requestBody)? As long as that check exists I'm not sure how to avoid both recursive steps.

This does however give the proper diff
image

This does need more tests, but I'd like another pair of eyes too look at the approach @EwenQuim if you don't mind. Could you please take a look. I'm sure this can be simplified. I've been staring at it too long at this point.

@epentland feel free to chime since you brought this to our attention

Thank you

@dylanhitt
Copy link
Collaborator Author

Was able to drop the need to recurse twice on request bodies by passing a the tag back with a struct of string and schemaRef

openapi.go Outdated Show resolved Hide resolved
@@ -67,6 +161,8 @@ func TestServer_generateOpenAPI(t *testing.T) {
require.NotNil(t, document.Paths.Find("/"))
require.Nil(t, document.Paths.Find("/unknown"))
require.NotNil(t, document.Paths.Find("/post"))
require.Equal(t, document.Paths.Find("/post").Post.Responses.Value("200").Value.Content["application/json"].Schema.Value.Type, "array")
require.Equal(t, document.Paths.Find("/post").Post.Responses.Value("200").Value.Content["application/json"].Schema.Value.Items.Ref, "#/components/schemas/MyStruct")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Need to do more to validate the actual schema reference here

openapi.go Outdated Show resolved Hide resolved
@dylanhitt dylanhitt requested a review from EwenQuim July 1, 2024 15:15
@dylanhitt
Copy link
Collaborator Author

@EwenQuim when you have a moment to do you mind taking a look.

Ignore this error as it is not possible with the current
instationation of of the openapi generator used. Unless
cyclic err is set or a custom schema customizer is provided
it is not possible to error.
if v == nil {
return "unknown-interface"
s.getOrCreateSchema("unknown-interface", struct{}{})
Copy link
Collaborator Author

@dylanhitt dylanhitt Jul 1, 2024

Choose a reason for hiding this comment

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

Need test to ensure an unknown-interface component schema is being added to the spec via test. I did not pick this up until my own diff

@EwenQuim
Copy link
Member

EwenQuim commented Jul 1, 2024

I'll look at it omorrow morning, thanks for this investigation.

It is indeed complex.

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.

None yet

2 participants