Skip to content

Added support for external references through the --import-mapping option#204

Merged
3 commits merged into
oapi-codegen:masterfrom
dududko:external-imports-v3-known-imports
Jul 9, 2020
Merged

Added support for external references through the --import-mapping option#204
3 commits merged into
oapi-codegen:masterfrom
dududko:external-imports-v3-known-imports

Conversation

@dududko
Copy link
Copy Markdown
Contributor

@dududko dududko commented Jul 6, 2020

In this PR I added a support for external references. The support is added with the use of --import-mapping option - a json that specifies the mapping from external reference to the golang package path. This will allow users to specify two openapi specs A and B, where A references B; generate golang package for spec B and then generate a valid golang package for spec A, which contains imports of a package B and 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 %d is a serial number.

I chose to call the option --import-mapping due 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.

@dududko dududko force-pushed the external-imports-v3-known-imports branch from 1e1a56d to 906becb Compare July 6, 2020 12:23
@dududko dududko force-pushed the external-imports-v3-known-imports branch from 906becb to bbff877 Compare July 6, 2020 12:32
@dududko
Copy link
Copy Markdown
Contributor Author

dududko commented Jul 6, 2020

Implements #42, #195

The solution is similar to #48

@utx0
Copy link
Copy Markdown

utx0 commented Jul 8, 2020

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.

@ghost
Copy link
Copy Markdown

ghost commented Jul 9, 2020

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.

@ghost ghost merged commit 3999601 into oapi-codegen:master Jul 9, 2020
@dududko
Copy link
Copy Markdown
Contributor Author

dududko commented Jul 10, 2020

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.

@ghost
Copy link
Copy Markdown

ghost commented Jul 10, 2020 via email

@dududko
Copy link
Copy Markdown
Contributor Author

dududko commented Jul 10, 2020

That stinks about the validator

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 getkin is using. I want to just read the file, unmarshal it's content and then decide if it should be validated, if all dependent specs should be parsed and etc.

Hope one day someone will refactor all this code.

adrianpk pushed a commit to foorester/oapi-codegen that referenced this pull request Jan 16, 2024
…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
This pull request was closed.
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