Skip to content

Commit

Permalink
[Go] add generateMarshalJSON key for additional-properties settings (#…
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
kokoichi206 committed Nov 2, 2023
1 parent fbbfa12 commit e4cfd62
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/generators/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,7 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -667,7 +668,12 @@ public ModelsMap postProcessModels(ModelsMap objs) {
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)
Expand Down Expand Up @@ -809,6 +815,10 @@ public void setWithGoMod(boolean withGoMod) {
this.withGoMod = withGoMod;
}

public void setGenerateMarshalJSON(boolean generateMarshalJSON) {
this.generateMarshalJSON = generateMarshalJSON;
}

@Override
public String toDefaultValue(Schema schema) {
schema = unaliasSchema(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public GoClientCodegen() {
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);
}

Expand Down Expand Up @@ -272,6 +273,10 @@ public void processOpts() {
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())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ protected void verifyOptions() {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -58,6 +59,7 @@ public Map<String, String> createOptions() {
.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();
Expand Down

0 comments on commit e4cfd62

Please sign in to comment.