From cc9caca868660c9748e3f02ba8e8021bfc9057a2 Mon Sep 17 00:00:00 2001 From: Slavek Kabrda Date: Mon, 27 May 2019 13:24:17 +0200 Subject: [PATCH] [Golang][client] Allow generating go client code as a submodule. --- docs/generators/go.md | 1 + .../org/openapitools/codegen/CodegenConstants.java | 3 +++ .../codegen/languages/GoClientCodegen.java | 13 +++++++++++++ .../src/main/resources/go/go.mod.mustache | 2 +- .../codegen/go/GoClientOptionsTest.java | 2 ++ .../codegen/options/GoClientOptionsProvider.java | 2 ++ 6 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/generators/go.md b/docs/generators/go.md index 07cdb2fdd793..ec1ecbebeeb1 100644 --- a/docs/generators/go.md +++ b/docs/generators/go.md @@ -10,6 +10,7 @@ sidebar_label: go |packageName|Go package name (convention: lowercase).| |openapi| |packageVersion|Go package version.| |1.0.0| |hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true| +|isGoSubmodule|whether the generated Go module is a submodule| |false| |withGoCodegenComment|whether to include Go codegen comment to disable Go Lint and collapse by default GitHub in PRs and diffs| |false| |withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false| |prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java index 75fcb1cba689..56d1f608f7ec 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java @@ -69,6 +69,9 @@ public class CodegenConstants { public static final String WITH_GO_CODEGEN_COMMENT = "withGoCodegenComment"; public static final String WITH_GO_CODEGEN_COMMENT_DESC = "whether to include Go codegen comment to disable Go Lint and collapse by default GitHub in PRs and diffs"; + public static final String IS_GO_SUBMODULE = "isGoSubmodule"; + public static final String IS_GO_SUBMODULE_DESC = "whether the generated Go module is a submodule"; + public static final String GROUP_ID = "groupId"; public static final String GROUP_ID_DESC = "groupId in generated pom.xml"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java index af79157f93aa..2ea1a2918c85 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java @@ -33,6 +33,7 @@ public class GoClientCodegen extends AbstractGoCodegen { protected String packageVersion = "1.0.0"; protected String apiDocPath = "docs/"; protected String modelDocPath = "docs/"; + protected boolean isGoSubmodule = false; public static final String WITH_GO_CODEGEN_COMMENT = "withGoCodegenComment"; public static final String WITH_XML = "withXml"; @@ -51,6 +52,7 @@ public GoClientCodegen() { // default HIDE_GENERATION_TIMESTAMP to true hideGenerationTimestamp = Boolean.TRUE; + cliOptions.add(CliOption.newBoolean(CodegenConstants.IS_GO_SUBMODULE, CodegenConstants.IS_GO_SUBMODULE_DESC)); cliOptions.add(CliOption.newBoolean(WITH_GO_CODEGEN_COMMENT, "whether to include Go codegen comment to disable Go Lint and collapse by default GitHub in PRs and diffs")); cliOptions.add(CliOption.newBoolean(WITH_XML, "whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)")); @@ -111,6 +113,13 @@ public void processOpts() { additionalProperties.put(WITH_XML, "true"); } } + + if (additionalProperties.containsKey(WITH_GO_CODEGEN_COMMENT)) { + setIsGoSubmodule(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.IS_GO_SUBMODULE).toString())); + if (isGoSubmodule) { + additionalProperties.put(CodegenConstants.IS_GO_SUBMODULE, "true"); + } + } } /** @@ -184,4 +193,8 @@ public void setPackageVersion(String packageVersion) { this.packageVersion = packageVersion; } + public void setIsGoSubmodule(boolean isGoSubmodule) { + this.isGoSubmodule = isGoSubmodule; + } + } diff --git a/modules/openapi-generator/src/main/resources/go/go.mod.mustache b/modules/openapi-generator/src/main/resources/go/go.mod.mustache index bc919e42e0e5..ec4310dd5932 100644 --- a/modules/openapi-generator/src/main/resources/go/go.mod.mustache +++ b/modules/openapi-generator/src/main/resources/go/go.mod.mustache @@ -1,4 +1,4 @@ -module github.com/{{gitUserId}}/{{gitRepoId}} +module github.com/{{gitUserId}}/{{gitRepoId}}{{#isGoSubmodule}}/{{packageName}}{{/isGoSubmodule}} require ( github.com/antihax/optional v0.0.0-20180406194304-ca021399b1a6 diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java index 7bdf5afbb60c..634fcc455063 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java @@ -52,6 +52,8 @@ protected void setExpectations() { times = 1; clientCodegen.setPrependFormOrBodyParameters(Boolean.valueOf(GoClientOptionsProvider.PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)); times = 1; + clientCodegen.setIsGoSubmodule(Boolean.valueOf(GoClientOptionsProvider.IS_GO_SUBMODULE_VALUE)); + times = 1; }}; } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java index 9e79d0ea0a03..39dc5d06ab80 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java @@ -29,6 +29,7 @@ public class GoClientOptionsProvider implements OptionsProvider { public static final boolean WITH_GO_CODEGEN_COMMENT_VALUE = true; public static final boolean WITH_XML_VALUE = true; public static final Boolean PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = true; + public static final boolean IS_GO_SUBMODULE_VALUE = true; @Override public String getLanguage() { @@ -45,6 +46,7 @@ public Map createOptions() { .put(CodegenConstants.WITH_GO_CODEGEN_COMMENT, "true") .put(CodegenConstants.WITH_XML, "true") .put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, "true") + .put(CodegenConstants.IS_GO_SUBMODULE, "true") .build(); }