这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@iwong-isp
Copy link
Collaborator

Addresses two issues with stable/consistent OpenAPI generation:

Properties will now output in lexicographical order for consistency. Operation parameters will honor the order they're defined in the struct.

A new example called bookstore is also added to demo a slightly more complex OpenAPI configuration where the schema ordering matters.

Copy link
Owner

@danielgtaylor danielgtaylor left a comment

Choose a reason for hiding this comment

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

Awesome work! 👍 Ordering by Go struct field definition is a good way to quickly fix this bug. I took a quick peek at the router, resource, operation, request, response and I think this PR fixes the only things preventing stable OpenAPI generation!

Definitely not a blocker, but one improvement I can think of for the future might be to order them based on their appearance in the URL template string, regardless of struct order. That way any inconsistency in the Go code ordering would not impact the generated parameter order given two operations with the same URL template. That's more difficult/complex to implement though and I think the current implementation is good.


var p opschema
json.Unmarshal(openapi, &p)
assert.Equal(t, p.Parameters, []parameters{{Name: "menu-category"}, {Name: "item-id"}})
Copy link
Owner

Choose a reason for hiding this comment

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

non-blocking: there's a 'JSONEq' that could be useful here. Something like:

assert.JSONEq(t, `[{"name": "menu-category", ...}]`, openapi)`

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I considered using that, but unfortunately, the output of the gabs.Search ends at the operation level. This results in openapi containing the full spec for the get operation (ex. responses, additional properties for a parameter). JSONEq only returns true if the entire JSON payload matches (ignoring order). Given we're only interested in using the name to check order was correctly preserved, I decided with a temp type to only parse the necessary field.

@codecov
Copy link

codecov bot commented Sep 30, 2022

Codecov Report

Base: 87.18% // Head: 87.23% // Increases project coverage by +0.04% 🎉

Coverage data is based on head (1af10e9) compared to base (5ed156c).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #71      +/-   ##
==========================================
+ Coverage   87.18%   87.23%   +0.04%     
==========================================
  Files          25       25              
  Lines        2138     2146       +8     
==========================================
+ Hits         1864     1872       +8     
  Misses        193      193              
  Partials       81       81              
Impacted Files Coverage Δ
operation.go 86.66% <100.00%> (+0.16%) ⬆️
patch.go 86.02% <100.00%> (+0.10%) ⬆️
resolver.go 88.54% <100.00%> (+0.22%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Owner

@danielgtaylor danielgtaylor left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@iwong-isp iwong-isp merged commit 1519e49 into main Sep 30, 2022
@iwong-isp iwong-isp deleted the iwong/openapi-ordering branch September 30, 2022 22:44
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.

3 participants