-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[core][maven][gradle] User-defined server variable substitutions #3363
Conversation
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
This is a 3.x enhancement (for server variable substitutions). See: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#server-object-example |
How does you change affect the {
"openapi" : "3.0.1",
"info" : {
"title" : "OpenAPI Petstore",
"description" : "This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.",
"license" : {
"name" : "Apache-2.0",
"url" : "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version" : "1.0.0"
},
"servers" : [ {
"url": "https://{variant}.example.com/",
"description": "The production API server",
"variables": {
"variant": {
"default": "v3",
"enum": ["v1", "v2", "v3"],
"description": "The version you want to work with"
}
}
}],
"paths" : {}
} I was thinking that a case like this create 3 server-urls:
But maybe this is not the way it works. Second question related to Is there some validation that the value provided are compatible with what is defined in the OpenAPI spec? According to the
so in my previous example, it is not allowed to set the value to |
@jmini thanks for the review. The enums are allowed values. Do you think we should warn if the value doesn't exist? I'd like to use the strict spec option for that validation, but I would need to do the rework of the static helpers to do it in a good way. A warning log might be the lowest impact. I can add that later today. To your enum question, your example would work to create a constrained set of server urls only if there's only enum options, and if we strictly stick to only those values. It's possible that someone may expose a v4 preview/beta version without updating the public doc in your example, and I don't want to be so strict that users wouldn't be able to generate the undocumented version. |
I would add, as well, we currently only pull the first server url and don't do any construction of server sets which involves enums (at least not where I can tell) |
I had some stuff come up. Will try to add that warning in the next day or so. |
I added support for server variables in |
@TiFu looking over your changes, it seems we'll want to figure out how to get the options passed by the user to the templates. We could do that on a separate PR. This PR allows users to define the values for a variable, so that the baseUrl which is generally passed into an API client targets the desired version of the server URL. |
@TiFu after digging into how I'd pass these values to the template, I began to see your concern. I need to rename |
…ates in the "servers" array
modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
Show resolved
Hide resolved
modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
Show resolved
Hide resolved
Wow this is exactly what is needed, hopefully it will be merged soon. |
* master: (122 commits) Fix #3604 by adding undefined as return type to headers and credentials methods in runtime.ts (#3605) Prepare 4.1.1-SNAPSHOT (#3603) Prepare 4.1.0 release (#3597) [java][client][jax-rs] Add a constant for Jackson @JsonProperty (#3560) restore openapi3 petstore.yaml (#3590) Add a new NodeJS Express server generator (#3567) [C#][client][csharp-netcore] Fix csharp netcore defaultheaders (#3562) Fix issue deserializing to nullptr (#3572) [OCaml] Add file post-processing (#3583) [dart2] Fix up test code generation, double deserialization, 'new' keyword deprecation (#3576) Run Qt5 client sample test (#3415) typescript-fetch: allow configuration of headers and credentials (#3586) using partials in ruby api_client (#3564) [OCaml] Added optional params support in API operations (#3568) [Rust Server] Generate valid Rustdocs for lazy_static items (#3556) Fix NPM build for Typescript-fetch (#3403) Expand path templates via resttemplate's uriTemplateHandler (#3500) Readme updated with a new tutorial and company using OpenAPI Generator (#3566) Fix logic of `getNullableType` of csharp server and client. (#3537) [Ruby] clean up Ruby dev dependencies (#3551) ...
Moved maven implementation to #3609 due to CI issues with resolving the maven plugin when it is a SNAPSHOT. |
) (#3609) * Adds server variables overrides option to the Maven plugin
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
,./bin/openapi3/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\
. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first.master
,4.1.x
,5.0.x
. Default:master
.Description of the PR
This provides an initial capability for users to define values for server variable substitutions.
Given a spec with a server URL definition such as:
The default will be
https://petstore.swagger.io/v2
, but we can now generate with user-declared options. For example:defaults (generates targeting
https://petstore.swagger.io/v2
) :user defined values (generates targeting
https://petstore.local/api
):The results:
The design/usage of
URLPathUtils.getServerURL
was a little weird (it offloads generation logic to a static utility). Rather than rewriting this, I've simply passed the overrides to it. I anticipate some URLs being missed due to the static method call, but we can address these as found. I hope to have workflows rewritten by the 5.0 release and will refactorURLPathUtils.getServerURL
at some point.cc @OpenAPITools/generator-core-team @OpenAPITools/openapi-generator-collaborators