Skip to content

Generate enum types from schemas in paths - #654

Closed
AlainODea wants to merge 3 commits into
oapi-codegen:mainfrom
AlainODea:ao-types-in-paths
Closed

AlainODea wants to merge 3 commits into
oapi-codegen:mainfrom
AlainODea:ao-types-in-paths

Conversation

@AlainODea

Copy link
Copy Markdown

If defining types in the schema within the parameters, responses, or requestBody within the paths of the spec, generate enums for them.

Also gather deeply nested enums in component responses or requestBodies.

Fixes #399 #467 #513 #626

If defining types in the schema within the parameters, responses, or requestBody within the paths of the spec, generate enums for them.

Also gather deeply nested enums in component responses or requestBodies.
@AlainODea

Copy link
Copy Markdown
Author

In retrospect, this breaks some subtle things that don't appear to have functional impact. internal/test/strict-server/client/client.go generates internal/test/strict-server/client/client.gen.go and it ends up with this strange diff after my change (dropping a useless Empty type in place of Reusableresponse):

diff --git a/internal/test/strict-server/client/client.gen.go b/internal/test/strict-server/client/client.gen.go
index 6c02ddf..7ddcd94 100644
--- a/internal/test/strict-server/client/client.gen.go
+++ b/internal/test/strict-server/client/client.gen.go
@@ -22,8 +22,8 @@ type Example struct {
        Value *string `json:"value,omitempty"`
 }
 
-// Reusableresponse defines model for reusableresponse.
-type Reusableresponse = Example
+// Empty defines model for .
+type Empty = int
 
 // MultipleRequestAndResponseTypesTextBody defines parameters for MultipleRequestAndResponseTypes.
 type MultipleRequestAndResponseTypesTextBody = string

AlainODea added 2 commits July 1, 2022 22:49
These all appear to be non-impacting changes as the types altered or omitted are not used. It also introduces an Empty type of some form regardless, which may be harmless.
This change brings back some generated types that are generated by the code on master. It also brings in some eccentric anonymous types and attending getter/setter and marshalling code.

@jamietanna jamietanna left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This doesn't quite solve #626, as in this case we get a:

// N200Health defines model for 200.Health.
type N200Health string

Whereas I'd expect a struct

@AlainODea

Copy link
Copy Markdown
Author

This doesn't quite solve #626, as in this case we get a:

// N200Health defines model for 200.Health.
type N200Health string

Whereas I'd expect a struct

Good catch! I had missed that. I'll take another shot at this and see where I can get.

Thank you for the code review.

@jamietanna

Copy link
Copy Markdown
Member

@AlainODea no worries! Mind marking this as a draft?

(I've also spotted that there may be cases where we're not (in the existing code) generating enums from nested objects - I'll try and get an issue raised for it soon)

@AlainODea
AlainODea marked this pull request as draft July 25, 2022 19:38
@discordianfish

Copy link
Copy Markdown

Are you still working on this? Would be great to see this fixed.

@AlainODea

Copy link
Copy Markdown
Author

Are you still working on this? Would be great to see this fixed.

I would love to. Unfortunately, my immediate need for this has passed and I'm swamped with other priorities at work.

There are a number of related PRs like Jamie's that take a more methodical approach to expanding support for types outside of schemas.

In the meantime, the best approach might be to manually or automatically transform the OpenAPI specs you need by moving things to schemas.

@mromaszewicz

Copy link
Copy Markdown
Member

Thank you for submitting this PR, @AlainODea — but we need to close it because all three cases you targeted (inline enums in path params, requestBodies, and responses) now generate correctly. Three independent fixes landed: commit 14fc88e2 (inline param enums, June 2022), commit 270581d0 (GenerateTypesForPaths() for response bodies, Nov 2022), and commit 81b9d952 (the Apr 2026 anonymous-schema-hoisting overhaul that feeds op-derived types into GenerateEnums()). A repro with the patterns from your PR generates correct named enum types.

Apologies for the long wait — your PR pointed at a real gap, even if the eventual fix took several rounds and a different shape.

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.

Issue generating enum in paths

4 participants