[Go] add generateMarshalJSON key for additional-properties settings (#16962)

* [add] additionalProperties about whether generating MarshalJSON (#16948)

* [change] key from skipGeneratingMarshalJSON to generateMarshalJSON (#16948)

* [test] modify unit tests (#16948)

* [fix] default value (#16948)

* [update] samples (#16948)

* [fix] document (#16948)
This commit is contained in:
TAKAHiRO TOMiNAGA 2023-11-02 23:12:21 +09:00 committed by GitHub
parent fbbfa12096
commit e4cfd626c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 24 additions and 0 deletions

View File

@ -21,6 +21,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|disallowAdditionalPropertiesIfNotPresent|If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.|<dl><dt>**false**</dt><dd>The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.</dd><dt>**true**</dt><dd>Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.</dd></dl>|true|
|enumClassPrefix|Prefix enum with class name| |false|
|generateInterfaces|Generate interfaces for api classes| |false|
|generateMarshalJSON|Generate MarshalJSON method| |true|
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
|isGoSubmodule|whether the generated Go module is a submodule| |false|
|packageName|Go package name (convention: lowercase).| |openapi|

View File

@ -433,4 +433,7 @@ public class CodegenConstants {
public static final String FASTAPI_IMPLEMENTATION_PACKAGE = "fastapiImplementationPackage";
public static final String WITH_GO_MOD = "withGoMod";
public static final String GENERATE_MARSHAL_JSON = "generateMarshalJSON";
public static final String GENERATE_MARSHAL_JSON_DESC = "Generate MarshalJSON method";
}

View File

@ -50,6 +50,7 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
protected boolean structPrefix = false;
protected boolean generateInterfaces = false;
protected boolean withGoMod = false;
protected boolean generateMarshalJSON = true;
protected String packageName = "openapi";
protected Set<String> numberTypes;
@ -667,7 +668,12 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
model.isNullable = true;
model.anyOf.remove("nil");
}
if (generateMarshalJSON) {
model.vendorExtensions.put("x-go-generate-marshal-json", true);
}
}
// recursively add import for mapping one type to multiple imports
List<Map<String, String>> recursiveImports = objs.getImports();
if (recursiveImports == null)
@ -809,6 +815,10 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
this.withGoMod = withGoMod;
}
public void setGenerateMarshalJSON(boolean generateMarshalJSON) {
this.generateMarshalJSON = generateMarshalJSON;
}
@Override
public String toDefaultValue(Schema schema) {
schema = unaliasSchema(schema);

View File

@ -139,6 +139,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
cliOptions.add(disallowAdditionalPropertiesIfNotPresentOpt);
this.setDisallowAdditionalPropertiesIfNotPresent(true);
cliOptions.add(CliOption.newBoolean(WITH_GO_MOD, "Generate go.mod and go.sum", true));
cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_MARSHAL_JSON, CodegenConstants.GENERATE_MARSHAL_JSON_DESC, true));
this.setWithGoMod(true);
}
@ -272,6 +273,10 @@ public class GoClientCodegen extends AbstractGoCodegen {
additionalProperties.put(WITH_GO_MOD, true);
}
if (additionalProperties.containsKey(CodegenConstants.GENERATE_MARSHAL_JSON)) {
setGenerateMarshalJSON(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.GENERATE_MARSHAL_JSON).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())));

View File

@ -260,6 +260,7 @@ func (o *{{classname}}) Unset{{name}}() {
{{/required}}
{{/vars}}
{{#vendorExtensions.x-go-generate-marshal-json}}
func (o {{classname}}) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {
@ -268,6 +269,7 @@ func (o {{classname}}) MarshalJSON() ([]byte, error) {
return json.Marshal(toSerialize)
}
{{/vendorExtensions.x-go-generate-marshal-json}}
func (o {{classname}}) ToMap() (map[string]interface{}, error) {
toSerialize := {{#isArray}}make([]interface{}, len(o.Items)){{/isArray}}{{^isArray}}map[string]interface{}{}{{/isArray}}
{{#parent}}

View File

@ -52,5 +52,6 @@ public class GoClientOptionsTest extends AbstractOptionsTest {
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);
verify(clientCodegen).setGenerateMarshalJSON(GoClientOptionsProvider.GENERATE_MARSHAL_JSON_VALUE);
}
}

View File

@ -37,6 +37,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
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;
public static final boolean GENERATE_MARSHAL_JSON_VALUE = true;
@Override
public String getLanguage() {
@ -58,6 +59,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
.put(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT, "true")
.put(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, "true")
.put(CodegenConstants.WITH_GO_MOD, "true")
.put(CodegenConstants.GENERATE_MARSHAL_JSON, "true")
.put("generateInterfaces", "true")
.put("structPrefix", "true")
.build();