Added support to skip creation of go.mod and go.sum in the Go client 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
This commit is contained in:
Valerio Santinelli 2023-10-11 10:00:19 +02:00 committed by GitHub
parent fe55938363
commit e3958cba75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 67 additions and 2 deletions

View File

@ -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| |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| |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| |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| |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 ## IMPORT MAPPING

View File

@ -429,4 +429,6 @@ public class CodegenConstants {
"<li>setting additionalProperties: false in your schemas</li></ul>"; "<li>setting additionalProperties: false in your schemas</li></ul>";
public static final String FASTAPI_IMPLEMENTATION_PACKAGE = "fastapiImplementationPackage"; public static final String FASTAPI_IMPLEMENTATION_PACKAGE = "fastapiImplementationPackage";
public static final String WITH_GO_MOD = "withGoMod";
} }

View File

@ -49,6 +49,7 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
protected boolean enumClassPrefix = false; protected boolean enumClassPrefix = false;
protected boolean structPrefix = false; protected boolean structPrefix = false;
protected boolean generateInterfaces = false; protected boolean generateInterfaces = false;
protected boolean withGoMod = false;
protected String packageName = "openapi"; protected String packageName = "openapi";
protected Set<String> numberTypes; protected Set<String> numberTypes;
@ -804,6 +805,10 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
this.generateInterfaces = generateInterfaces; this.generateInterfaces = generateInterfaces;
} }
public void setWithGoMod(boolean withGoMod) {
this.withGoMod = withGoMod;
}
@Override @Override
public String toDefaultValue(Schema schema) { public String toDefaultValue(Schema schema) {
schema = unaliasSchema(schema); schema = unaliasSchema(schema);

View File

@ -56,6 +56,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
public static final String WITH_AWSV4_SIGNATURE = "withAWSV4Signature"; public static final String WITH_AWSV4_SIGNATURE = "withAWSV4Signature";
public static final String GENERATE_INTERFACES = "generateInterfaces"; public static final String GENERATE_INTERFACES = "generateInterfaces";
public static final String MODEL_FILE_FOLDER = "modelFileFolder"; public static final String MODEL_FILE_FOLDER = "modelFileFolder";
public static final String WITH_GO_MOD = "withGoMod";
protected String goImportAlias = "openapiclient"; protected String goImportAlias = "openapiclient";
protected boolean isGoSubmodule = false; protected boolean isGoSubmodule = false;
protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup
@ -137,6 +138,8 @@ public class GoClientCodegen extends AbstractGoCodegen {
disallowAdditionalPropertiesIfNotPresentOpt.setEnum(disallowAdditionalPropertiesIfNotPresentOpts); disallowAdditionalPropertiesIfNotPresentOpt.setEnum(disallowAdditionalPropertiesIfNotPresentOpts);
cliOptions.add(disallowAdditionalPropertiesIfNotPresentOpt); cliOptions.add(disallowAdditionalPropertiesIfNotPresentOpt);
this.setDisallowAdditionalPropertiesIfNotPresent(true); this.setDisallowAdditionalPropertiesIfNotPresent(true);
cliOptions.add(CliOption.newBoolean(WITH_GO_MOD, "Generate go.mod and go.sum", true));
this.setWithGoMod(true);
} }
/** /**
@ -262,6 +265,13 @@ public class GoClientCodegen extends AbstractGoCodegen {
modelFileFolder = additionalProperties.get(MODEL_FILE_FOLDER).toString(); 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 // add lambda for mustache templates to handle oneOf/anyOf naming
// e.g. []string => ArrayOfString // e.g. []string => ArrayOfString
additionalProperties.put("lambda.type-to-name", (Mustache.Lambda) (fragment, writer) -> writer.write(typeToName(fragment.execute()))); additionalProperties.put("lambda.type-to-name", (Mustache.Lambda) (fragment, writer) -> writer.write(typeToName(fragment.execute())));
@ -273,8 +283,10 @@ public class GoClientCodegen extends AbstractGoCodegen {
supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.go")); supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.go"));
supportingFiles.add(new SupportingFile("client.mustache", "", "client.go")); supportingFiles.add(new SupportingFile("client.mustache", "", "client.go"));
supportingFiles.add(new SupportingFile("response.mustache", "", "response.go")); supportingFiles.add(new SupportingFile("response.mustache", "", "response.go"));
supportingFiles.add(new SupportingFile("go.mod.mustache", "", "go.mod")); if ((boolean)additionalProperties.get(WITH_GO_MOD)) {
supportingFiles.add(new SupportingFile("go.sum.mustache", "", "go.sum")); 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(".travis.yml", "", ".travis.yml"));
supportingFiles.add(new SupportingFile("utils.mustache", "", "utils.go")); supportingFiles.add(new SupportingFile("utils.mustache", "", "utils.go"));
} }

View File

@ -291,4 +291,46 @@ public class GoClientCodegenTest {
"httpRes, err := apiClient.PetAPI.PetDelete(context.Background()).Execute()"); "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);
}
} }

View File

@ -51,5 +51,6 @@ public class GoClientOptionsTest extends AbstractOptionsTest {
verify(clientCodegen).setStructPrefix(GoClientOptionsProvider.STRUCT_PREFIX_VALUE); verify(clientCodegen).setStructPrefix(GoClientOptionsProvider.STRUCT_PREFIX_VALUE);
verify(clientCodegen).setWithAWSV4Signature(GoClientOptionsProvider.WITH_AWSV4_SIGNATURE); verify(clientCodegen).setWithAWSV4Signature(GoClientOptionsProvider.WITH_AWSV4_SIGNATURE);
verify(clientCodegen).setUseOneOfDiscriminatorLookup(GoClientOptionsProvider.USE_ONE_OF_DISCRIMINATOR_LOOKUP_VALUE); verify(clientCodegen).setUseOneOfDiscriminatorLookup(GoClientOptionsProvider.USE_ONE_OF_DISCRIMINATOR_LOOKUP_VALUE);
verify(clientCodegen).setWithGoMod(GoClientOptionsProvider.WITH_GO_MOD_VALUE);
} }
} }

View File

@ -36,6 +36,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
public static final boolean GENERATE_INTERFACES_VALUE = true; public static final boolean GENERATE_INTERFACES_VALUE = true;
public static final boolean DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_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 USE_ONE_OF_DISCRIMINATOR_LOOKUP_VALUE = true;
public static final boolean WITH_GO_MOD_VALUE = true;
@Override @Override
public String getLanguage() { public String getLanguage() {
@ -56,6 +57,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
.put(CodegenConstants.WITH_AWSV4_SIGNATURE_COMMENT, "true") .put(CodegenConstants.WITH_AWSV4_SIGNATURE_COMMENT, "true")
.put(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT, "true") .put(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT, "true")
.put(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, "true") .put(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, "true")
.put(CodegenConstants.WITH_GO_MOD, "true")
.put("generateInterfaces", "true") .put("generateInterfaces", "true")
.put("structPrefix", "true") .put("structPrefix", "true")
.build(); .build();