Added support for external references through the --import-mapping option#204
Conversation
1e1a56d to
906becb
Compare
906becb to
bbff877
Compare
|
Hey @dududko thanks for the PR, code looks good to me. I will alsov test the feature out over this week if I can find the space. @deepmap-marcinr be great to get some feedback from you on this one? This is a much needed feature for many. |
|
This is a very interesting way of doing it! I like the notion of import mapping. I'm going to merge this and change it around a little bit, for example, import maping being JSON is kinda tedious to type in on command lines, so I might just make it key1:value1,key2:value2 with some provision for escaping commas. |
|
I am glad to hear that you accepted this new feature. So of we will add the following lines of code to the test:
I think is should be fixed in |
|
Thanks. That stinks about the validator. I might have to write a function
to fetch external dependencies and merge them together. I already have some
rather tricky code written which can merge two specs and resolve type
collisions, might as well use it.
…On Fri, Jul 10, 2020 at 10:07 AM Dudko Alexey ***@***.***> wrote:
I am glad to hear that you accepted this new feature.
I think it is important to tell you that recently I found out that if
there are external references in openapi specification, then the generated
swaggerSpec variable is not valid. As a result, is is not possible to use
the middleware.OapiRequestValidator.
So of we will add the following lines of code to the test:
swagger, err := api.GetSwagger()
if err != nil {
panic(err)
}
data, err := swagger.MarshalJSON()
if err != nil {
panic(err)
}
fmt.Println(string(data))
GetSwagger function will work, but MarshalJSON will panic
panic: error loading Swagger: Encountered non-allowed external reference: 'bitbucket.org/projectplato/rules_openapi/examples/external_ref/packageB/spec.yaml#/components/schemas/ObjectB'
I think is should be fixed in github.com/getkin/kin-openapi, otherwise
the embedding logic need to be implemented in this repo.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#204 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ALKC5DD6CC76QSJRVTB7YPDR25DEPANCNFSM4ORRRE5Q>
.
|
To be honest I hate the fact, that there is no single struct in go that represents the openapi container as is with all optional/required attributes. Instead we are working with internal representation that Hope one day someone will refactor all this code. |
…tion (oapi-codegen#204) * use kin-openapi only to read openapi file * added the support of external references throug the --import-mapping option * added example for external reference
In this PR I added a support for external references. The support is added with the use of
--import-mappingoption - a json that specifies the mapping from external reference to the golang package path. This will allow users to specify two openapi specsAandB, whereAreferencesB; generate golang package for specBand then generate a valid golang package for specA, which contains imports of a packageBand uses it's types.You can see the example here
internal/test/externalref/.Since the external packages that are imported can have the same package name, I sort imported package paths and them give them a uniques alias
externalRef%d, where%dis a serial number.I chose to call the option
--import-mappingdue to the similar option in vanilla openapi generator.https://openapi-generator.tech/docs/customization/#bringing-your-own-models
Feel free to review and comment this PR.
P.S. I also tried to implement the embedding of external references instead of importing them, but it introduces too many changes... It will require the major refactoring to have this feature, this is why I did not complete it.