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 types and standard structs #134

Merged
merged 9 commits into from
Jul 16, 2024

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.

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.

@dylanhitt
Copy link
Collaborator Author

Sorry for the spam 😓. Messed some things up while getting my fork ready for a build.

@dylanhitt
Copy link
Collaborator Author

@EwenQuim any word on this?

@EwenQuim EwenQuim mentioned this pull request Jul 16, 2024
@EwenQuim
Copy link
Member

Ready to review it, I'd like to merge #137 just before this one, so I'll be able to work on Multi Return OpenAPI spec

@EwenQuim EwenQuim changed the title fix: differentiate between array typres and standard structs fix: differentiate between array types and standard structs Jul 16, 2024
@EwenQuim
Copy link
Member

Mmmh maybe I'll merge this before #137 finally...

Comment on lines +245 to +252
case reflect.Slice, reflect.Array:
item := dive(s, t.Elem(), tag, maxDepth-1)
tag.name = item.name
tag.Value = &openapi3.Schema{
Type: "array",
Items: &item.SchemaRef,
}
return dive(t.Elem(), maxDepth-1)
return tag
Copy link
Member

Choose a reason for hiding this comment

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

Perfect, you really dived into the code! 😉

@EwenQuim EwenQuim merged commit 507df2e into go-fuego:main Jul 16, 2024
5 checks passed
@dylanhitt dylanhitt deleted the fix/array_type branch July 16, 2024 12:32
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