Skip to content

Commit

Permalink
[Rust Server] Don't change API version (#5458)
Browse files Browse the repository at this point in the history
Don't change the API version which is exposed in `crate::API_VERSION`.

- If the API version isn't set, we don't expose it.

- If it is set, we leave it well alone.

- Always pass a package version:
 - Pass in the passed package version by preference
 - Pass in the API version if it's not.
   - If the API version isn't set, we use 1.0.0
   - If it is set, and isn't a valid semver, we append .0 such that we have
     something with three digits (so that an API version of 1 works).
  • Loading branch information
richardwhiuk authored Mar 1, 2020
1 parent dc0da57 commit 8f8443d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ public void processOpts() {
additionalProperties.put("modelDocPath", modelDocPath);

additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion);
additionalProperties.put("externCrateName", externCrateName);
}

Expand Down Expand Up @@ -323,19 +322,26 @@ public String getHelp() {

@Override
public void preprocessOpenAPI(OpenAPI openAPI) {

Info info = openAPI.getInfo();
List<String> versionComponents = new ArrayList<>(Arrays.asList(info.getVersion().split("[.]")));
if (versionComponents.size() < 1) {
versionComponents.add("1");
}
while (versionComponents.size() < 3) {
versionComponents.add("0");
}
info.setVersion(StringUtils.join(versionComponents, "."));

URL url = URLPathUtils.getServerURL(openAPI, serverVariableOverrides());
additionalProperties.put("serverHost", url.getHost());
additionalProperties.put("serverPort", URLPathUtils.getPort(url, 80));

if (packageVersion == null || "".equals(packageVersion)) {
List<String> versionComponents = new ArrayList<>(Arrays.asList(info.getVersion().split("[.]")));
if (versionComponents.size() < 1) {
versionComponents.add("1");
}
while (versionComponents.size() < 3) {
versionComponents.add("0");
}

setPackageVersion(StringUtils.join(versionComponents, "."));
}

additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "{{{packageName}}}"
version = {{#packageVersion}}"{{{packageVersion}}}"{{/packageVersion}}{{^packageVersion}}"{{{appVersion}}}"{{/packageVersion}}{{! Fill in with packageVersion if defined, which can be passed via a command line argument, Else use appVersion from the Open API spec, which defaults to 1.0.0 if not present.}}
version = "{{{packageVersion}}}"
authors = [{{#infoEmail}}"{{{infoEmail}}}"{{/infoEmail}}]
{{#appDescription}}
description = "{{{appDescription}}}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ pub use swagger::{ApiError, ContextWrapper};
pub use futures::Future;

pub const BASE_PATH: &'static str = "{{{basePathWithoutHost}}}";
{{#appVersion}}
pub const API_VERSION: &'static str = "{{{appVersion}}}";
{{/appVersion}}

{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}
#[derive(Debug, PartialEq)]
Expand Down

0 comments on commit 8f8443d

Please sign in to comment.