kin-openapi icon indicating copy to clipboard operation
kin-openapi copied to clipboard

Add function to check if a schema matches one in root document

Open percivalalb opened this issue 1 year ago • 2 comments

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.~~ https://github.com/deepmap/oapi-codegen/pull/1573

Note: currently this is built on v0.123 as v0.124 is not compatible with oapi-codegen at the moment.

percivalalb avatar Apr 22 '24 20:04 percivalalb

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.

fenollp avatar Apr 29 '24 16:04 fenollp

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 avatar Apr 30 '24 20:04 percivalalb

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 avatar Jun 01 '24 16:06 percivalalb

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.

fenollp avatar Jun 02 '24 12:06 fenollp

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

percivalalb avatar Jun 02 '24 15:06 percivalalb

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

Edit: Added an example to the READMD.md

percivalalb avatar Jun 15 '24 07:06 percivalalb