-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add optional type generation for inline schemas #2366
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
mromaszewicz
wants to merge
5
commits into
oapi-codegen:main
Choose a base branch
from
mromaszewicz:fix/issue-1139
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
68abea9
Add optional type generation for inline schemas
mromaszewicz f6c3b31
Update internal/test/anonymous_inner_hoisting/global/spec.yaml
mromaszewicz 107232d
Update hoisted typedef comment
mromaszewicz 68c3d9c
Merge remote-tracking branch 'origin/main' into fix/issue-1139
mromaszewicz 476b896
Revert "Update hoisted typedef comment"
mromaszewicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package: global | ||
| output: client.gen.go | ||
| generate: | ||
| models: true | ||
| client: true | ||
| output-options: | ||
| generate-types-for-anonymous-schemas: true | ||
279 changes: 279 additions & 0 deletions
279
internal/test/anonymous_inner_hoisting/global/client.gen.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
internal/test/anonymous_inner_hoisting/global/client_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package global | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestHoistedTypesExist verifies that with output-options.generate-types-for- | ||
| // anonymous-schemas enabled, the inline schemas in spec.yaml become named | ||
| // Go types we can reference directly. The spec is the canonical issue #1139 | ||
| // shape: a response body using `allOf` to merge a $ref with sibling | ||
| // `properties:` containing an inline `data` object. | ||
| func TestHoistedTypesExist(t *testing.T) { | ||
| // Both the response root and the nested inline `data` schema should be | ||
| // emitted as named types — assigning a typed zero value would not | ||
| // compile if either were still anonymous structs. | ||
| var responseBody GetRolesId200JSONResponseBody | ||
| var dataField GetRolesId200JSONResponseBody_Data | ||
|
|
||
| // Field-level type identity: GetRolesId200JSONResponseBody.Data must be | ||
| // of the hoisted GetRolesId200JSONResponseBody_Data type. This | ||
| // assignment fails to compile if Data is still an anonymous struct. | ||
| responseBody.Data = dataField | ||
| _ = responseBody | ||
| } | ||
|
|
||
| func TestHoistedTypesRoundTrip(t *testing.T) { | ||
| body := GetRolesId200JSONResponseBody{ | ||
| Data: GetRolesId200JSONResponseBody_Data{ | ||
| Role: Role{Id: 7, Name: "admin"}, | ||
| }, | ||
| Ok: true, | ||
| } | ||
|
|
||
| encoded, err := json.Marshal(body) | ||
| require.NoError(t, err) | ||
|
|
||
| var decoded GetRolesId200JSONResponseBody | ||
| require.NoError(t, json.Unmarshal(encoded, &decoded)) | ||
| assert.Equal(t, body, decoded) | ||
| } |
2 changes: 1 addition & 1 deletion
2
...test/anonymous_inner_hoisting/generate.go → ...onymous_inner_hoisting/global/generate.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| package anonymous_inner_hoisting | ||
| package global | ||
|
|
||
| //go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=cfg.yaml spec.yaml |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, I'd prefer this by default - most folks don't control their specs, and so they get anonymous structs used which can be awkward
I'd personally swap this with a
compatibility-optionsto disable this functionalityUsing an OpenAPI Overlay is one way to get around this, but managing all areas this may apply will be quite hard
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have the opposite view.
In specs with lots of inline schemas, this positively litters the file with new type definitions, and in many cases, you don't need to ever refer to the type, so the code, IMO, is nicer. I'm open to defaulting it to be on, however, I dislike the output.
Whenever you have an inline schema in a request body, or a parameter, you will never need to create an instance of that type, deserialization does that for you.
The place where it is nice to have is when you have inline schemas in things you are constructing; usually response bodies, and in those cases, it's so much nicer to declare those specifically. I realize that some people don't control their specs.