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

openapi3: introduce ReferencesComponentInRootDocument(doc *T, ref componentRef) (string, bool) #945

Merged
merged 11 commits into from
Jun 16, 2024

Conversation

percivalalb
Copy link
Contributor

@percivalalb percivalalb commented Apr 22, 2024

This MR adds a function (ReferencesComponentInRootDocument) to check if a component reference references the same document or element as another component reference in the root document's #/components/<type>. If it does, it returns the name of it in the form #/components/<type>/NameXXX.

It does this by keeping track of the source file paths of schemas in multi-file'd specs.

This enables the amalgamation of schemas which reference the same underlying model which removes very annoying to use anonymous structs in generated models when using codegen tools like oapi-codegen.

TODO: add MR in oapi-codegen displaying improved model generation. oapi-codegen/oapi-codegen#1573

@fenollp
Copy link
Collaborator

fenollp commented Apr 29, 2024

Hello!
How about a func that, given a doc and a Schema, returns a *SchemaRef (a ptr under doc) or nil? Would this not cover your use case?

Note: the refs.go file is generated.

@percivalalb
Copy link
Contributor Author

Hello!

How about a func that, given a doc and a Schema, returns a *SchemaRef (a ptr under doc) or nil? Would this not cover your use case?

Note: the refs.go file is generated.

Yes, if it also returned the name in the #components/schemas/...

Input: doc, Schema
Output: name, SchemaRef

I'll have a look at moving it out of the gen'ed file

@percivalalb
Copy link
Contributor Author

I've moved the alternations out of the code gen'ed files, sorry I'm still getting familar with the project setup and just thrown everything in the helper file. I'd like to ad some unit tests of couse but hopefully this is more to your liking.

@percivalalb percivalalb force-pushed the alb/reconsole-schema-refs branch 2 times, most recently from 3897eef to 11a0e6f Compare June 2, 2024 11:04
@percivalalb percivalalb changed the title Add function to check if a schema matches one in root document Draft: Add function to check if a schema matches one in root document Jun 2, 2024
@fenollp
Copy link
Collaborator

fenollp commented Jun 2, 2024

This is looking pretty good!
Could you re-push the docs? It fails CI.
Also please send your tests away :) And an Example that shows usage of the function.

openapi3/helpers.go Outdated Show resolved Hide resolved
@percivalalb
Copy link
Contributor Author

percivalalb commented Jun 2, 2024

Things to do:

  • I think these snippets https://github.com/getkin/kin-openapi/pull/945/files#diff-035b395ae81006bbe6cd7144c686238113762fccdb051ddaef5d507560039ee6R540 which construct the refPath should be moved to a single function in resolveComponent. I believe componentPath should be the value we need already.
  • I'm not sure how this plays well with schema's loaded from memory (i.e. no paths)
  • Also not sure if it supports external referencing other spec files or URL references
  • Update ComponentType so that values are returned in camelCase - currently some of the values are invalid
  • Not sure if I should be using path or filepath to join path components. I think some of the uri are from the file system (so os dependant) but others are URLs so use /.
  • Need to add unit tests
  • Add example usage of function

Heads up I'll be away for 10 days, so I won't be developing this PR for a bit

It does this by keeping track of the source file paths of schemas in
multi-file'd specs.

This enables the amalgamation of schemas which reference the same underlying
model which removes *very annoying* to use anonymous structs in generated
models when using codegen tools like oapi-codegen.
And populate all ref types
@percivalalb
Copy link
Contributor Author

percivalalb commented Jun 15, 2024

Address most of the bullet points. Where is the best place for the example?

Edit: Added an example to the READMD.md

openapi3/helpers.go Outdated Show resolved Hide resolved
@percivalalb percivalalb changed the title Draft: Add function to check if a schema matches one in root document Add function to check if a schema matches one in root document Jun 15, 2024
@fenollp fenollp changed the title Add function to check if a schema matches one in root document openapi3: introduce ReferencesComponentInRootDocument(doc *T, ref componentRef) (string, bool) Jun 16, 2024
@fenollp fenollp merged commit 061c378 into getkin:master Jun 16, 2024
5 checks passed
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