Skip to content

Commit

Permalink
Added support to skip creation of go.mod and go.sum in the Go client …
Browse files Browse the repository at this point in the history
…generator (#16766)

This adds support to avoid generating go.mod and go.sum for Go client.

By default it is set to true to keep compatibility with previous version
of the tool.

It can be set to false using --additional-properties=withGoMod=false
  • Loading branch information
tanis2000 authored Oct 11, 2023
1 parent fe55938 commit e3958cb
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/generators/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|structPrefix|whether to prefix struct with the class name. e.g. DeletePetOpts => PetApiDeletePetOpts| |false|
|useOneOfDiscriminatorLookup|Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped.| |false|
|withAWSV4Signature|whether to include AWS v4 signature support| |false|
|withGoMod|Generate go.mod and go.sum| |true|
|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|

## IMPORT MAPPING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,6 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
"<li>setting additionalProperties: false in your schemas</li></ul>";

public static final String FASTAPI_IMPLEMENTATION_PACKAGE = "fastapiImplementationPackage";

public static final String WITH_GO_MOD = "withGoMod";
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
protected boolean enumClassPrefix = false;
protected boolean structPrefix = false;
protected boolean generateInterfaces = false;
protected boolean withGoMod = false;

protected String packageName = "openapi";
protected Set<String> numberTypes;
Expand Down Expand Up @@ -804,6 +805,10 @@ public void setGenerateInterfaces(boolean generateInterfaces) {
this.generateInterfaces = generateInterfaces;
}

public void setWithGoMod(boolean withGoMod) {
this.withGoMod = withGoMod;
}

@Override
public String toDefaultValue(Schema schema) {
schema = unaliasSchema(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
public static final String WITH_AWSV4_SIGNATURE = "withAWSV4Signature";
public static final String GENERATE_INTERFACES = "generateInterfaces";
public static final String MODEL_FILE_FOLDER = "modelFileFolder";
public static final String WITH_GO_MOD = "withGoMod";
protected String goImportAlias = "openapiclient";
protected boolean isGoSubmodule = false;
protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup
Expand Down Expand Up @@ -137,6 +138,8 @@ public GoClientCodegen() {
disallowAdditionalPropertiesIfNotPresentOpt.setEnum(disallowAdditionalPropertiesIfNotPresentOpts);
cliOptions.add(disallowAdditionalPropertiesIfNotPresentOpt);
this.setDisallowAdditionalPropertiesIfNotPresent(true);
cliOptions.add(CliOption.newBoolean(WITH_GO_MOD, "Generate go.mod and go.sum", true));
this.setWithGoMod(true);
}

/**
Expand Down Expand Up @@ -262,6 +265,13 @@ public void processOpts() {
modelFileFolder = additionalProperties.get(MODEL_FILE_FOLDER).toString();
}

if (additionalProperties.containsKey(WITH_GO_MOD)) {
setWithGoMod(Boolean.parseBoolean(additionalProperties.get(WITH_GO_MOD).toString()));
additionalProperties.put(WITH_GO_MOD, withGoMod);
} else {
additionalProperties.put(WITH_GO_MOD, true);
}

// 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 All @@ -273,8 +283,10 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.go"));
supportingFiles.add(new SupportingFile("client.mustache", "", "client.go"));
supportingFiles.add(new SupportingFile("response.mustache", "", "response.go"));
supportingFiles.add(new SupportingFile("go.mod.mustache", "", "go.mod"));
supportingFiles.add(new SupportingFile("go.sum.mustache", "", "go.sum"));
if ((boolean)additionalProperties.get(WITH_GO_MOD)) {
supportingFiles.add(new SupportingFile("go.mod.mustache", "", "go.mod"));
supportingFiles.add(new SupportingFile("go.sum.mustache", "", "go.sum"));
}
supportingFiles.add(new SupportingFile(".travis.yml", "", ".travis.yml"));
supportingFiles.add(new SupportingFile("utils.mustache", "", "utils.go"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,46 @@ public void verifyApiTestWithNullResponse() throws IOException {
"httpRes, err := apiClient.PetAPI.PetDelete(context.Background()).Execute()");
}

@Test
public void testAdditionalPropertiesWithGoMod() throws Exception {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("go")
.setInputSpec("src/test/resources/3_0/petstore_oas3_test.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
System.out.println(files);
files.forEach(File::deleteOnExit);

Path goModFile = Paths.get(output + "/go.mod");
TestUtils.assertFileExists(goModFile);
Path goSumFile = Paths.get(output + "/go.sum");
TestUtils.assertFileExists(goSumFile);
}

@Test
public void testAdditionalPropertiesWithoutGoMod() throws Exception {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("go")
.setInputSpec("src/test/resources/3_0/petstore_oas3_test.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"))
.addAdditionalProperty(GoClientCodegen.WITH_GO_MOD, false);

DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
System.out.println(files);
files.forEach(File::deleteOnExit);

Path goModFile = Paths.get(output + "/go.mod");
TestUtils.assertFileNotExists(goModFile);
Path goSumFile = Paths.get(output + "/go.sum");
TestUtils.assertFileNotExists(goSumFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ protected void verifyOptions() {
verify(clientCodegen).setStructPrefix(GoClientOptionsProvider.STRUCT_PREFIX_VALUE);
verify(clientCodegen).setWithAWSV4Signature(GoClientOptionsProvider.WITH_AWSV4_SIGNATURE);
verify(clientCodegen).setUseOneOfDiscriminatorLookup(GoClientOptionsProvider.USE_ONE_OF_DISCRIMINATOR_LOOKUP_VALUE);
verify(clientCodegen).setWithGoMod(GoClientOptionsProvider.WITH_GO_MOD_VALUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
public static final boolean GENERATE_INTERFACES_VALUE = true;
public static final boolean DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_VALUE = true;
public static final boolean USE_ONE_OF_DISCRIMINATOR_LOOKUP_VALUE = true;
public static final boolean WITH_GO_MOD_VALUE = true;

@Override
public String getLanguage() {
Expand All @@ -56,6 +57,7 @@ public Map<String, String> createOptions() {
.put(CodegenConstants.WITH_AWSV4_SIGNATURE_COMMENT, "true")
.put(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT, "true")
.put(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, "true")
.put(CodegenConstants.WITH_GO_MOD, "true")
.put("generateInterfaces", "true")
.put("structPrefix", "true")
.build();
Expand Down

0 comments on commit e3958cb

Please sign in to comment.