From e8c739e771b8bb976fe3600c9e7ecb1d324ef0e3 Mon Sep 17 00:00:00 2001 From: Beppe Catanese <1771700+gcatanese@users.noreply.github.com> Date: Tue, 31 Jan 2023 15:49:45 +0100 Subject: [PATCH] Read modelFileFolderPath from additionalProperties (#12536) --- .../codegen/languages/GoClientCodegen.java | 16 +++++++++++++++- .../codegen/go/GoClientCodegenTest.java | 10 ++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) 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 e89bd06e4ab..b345b935d96 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 @@ -50,10 +50,12 @@ public class GoClientCodegen extends AbstractGoCodegen { protected String packageVersion = "1.0.0"; protected String apiDocPath = "docs/"; protected String modelDocPath = "docs/"; + protected String modelFileFolder = null; public static final String WITH_XML = "withXml"; public static final String STRUCT_PREFIX = "structPrefix"; public static final String WITH_AWSV4_SIGNATURE = "withAWSV4Signature"; public static final String GENERATE_INTERFACES = "generateInterfaces"; + public static final String MODEL_FILE_FOLDER = "modelFileFolder"; protected String goImportAlias = "openapiclient"; protected boolean isGoSubmodule = false; protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup @@ -255,6 +257,10 @@ public class GoClientCodegen extends AbstractGoCodegen { .get(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT).toString())); } + if (additionalProperties.containsKey(MODEL_FILE_FOLDER)) { + modelFileFolder = additionalProperties.get(MODEL_FILE_FOLDER).toString(); + } + // add lambda for mustache templates to handle oneOf/anyOf naming // e.g. []string => ArrayOfString additionalProperties.put("lambda.type-to-name", (Mustache.Lambda) (fragment, writer) -> writer.write(typeToName(fragment.execute()))); @@ -301,9 +307,17 @@ public class GoClientCodegen extends AbstractGoCodegen { return outputFolder + File.separator; } + /** + * Location of created model files (it can be overriden using --additional-properties in openapi-generator-cli + */ @Override public String modelFileFolder() { - return outputFolder + File.separator; + String modelFileFolderPath = outputFolder + File.separator; + + if(modelFileFolder != null) { + modelFileFolderPath = modelFileFolderPath + modelFileFolder + File.separator; + } + return modelFileFolderPath; } @Override diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java index 63e47a7d635..74ddeb0c75d 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java @@ -50,6 +50,7 @@ public class GoClientCodegenTest { Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE); Assert.assertTrue(codegen.isHideGenerationTimestamp()); + Assert.assertNull(codegen.additionalProperties().get(GoClientCodegen.MODEL_FILE_FOLDER)); } @Test @@ -203,6 +204,15 @@ public class GoClientCodegenTest { TestUtils.assertFileContains(Paths.get(output + "/api_pet.go"), "type PetApiAddPetRequest struct"); } + @Test + public void testAdditionalPropertiesModelFileFolder() throws Exception { + final GoClientCodegen codegen = new GoClientCodegen(); + codegen.additionalProperties().put(GoClientCodegen.MODEL_FILE_FOLDER, "model_dir"); + codegen.processOpts(); + + Assert.assertEquals(codegen.modelFileFolder(), "generated-code/go/model_dir/"); + } + @Test public void verifyTestFile() throws IOException { File output = Files.createTempDirectory("test").toFile();