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

Reconcile references to remote schemas to models from the root spec #1573

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

percivalalb
Copy link

@percivalalb percivalalb commented Apr 22, 2024

For #1362

OpenAPI specifications can be spread across different files for maintainability. However this is not great for code-gen which can't recognise when two schema refs refer to the same model. This results in the code-gen using duplicating anonymous structs (which can't easily be used).

I have added a patch to kin upstream (getkin/kin-openapi#945) which identifies when a ref refers to a schema from the root #/components/schemas and makes use of the go model generated from that. I couldn't decide how much logic should exist there without exposing the internals of kin. I'm not sure if it accounts for every edge case, but certainly works for my case and doesn't break any of the existing example specs within this repo.

See f662fa7 which shows the change codegen change:

image

Of course the kin PR needs looking at first and once merged the go mod replacement directions need removing.

@@ -237,7 +237,7 @@ func PropertiesEqual(a, b Property) bool {
return a.JsonFieldName == b.JsonFieldName && a.Schema.TypeDecl() == b.Schema.TypeDecl() && a.Required == b.Required
}

func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
func GenerateGoSchema(sref *openapi3.SchemaRef, path []string, component bool) (Schema, error) {
Copy link
Author

Choose a reason for hiding this comment

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

component could do with a better name.

When it's true we are generating a go type for a schema in '#/components/schemas' the root document.

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

1 participant