Skip to content

Commit

Permalink
Read modelFileFolderPath from additionalProperties (OpenAPITools#12536)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcatanese authored Jan 31, 2023
1 parent a28772b commit e8c739e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -255,6 +257,10 @@ public void processOpts() {
.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())));
Expand Down Expand Up @@ -301,9 +307,17 @@ public String apiFileFolder() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void testInitialConfigValues() throws Exception {

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
Expand Down Expand Up @@ -203,6 +204,15 @@ public void testStructPrefix() throws IOException {
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();
Expand Down

0 comments on commit e8c739e

Please sign in to comment.