Skip to content

Commit 4616ff3

Browse files
committed
add generate unmarshal json option in go client generator
1 parent ba95e7b commit 4616ff3

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
442442
public static final String GENERATE_MARSHAL_JSON = "generateMarshalJSON";
443443
public static final String GENERATE_MARSHAL_JSON_DESC = "Generate MarshalJSON method";
444444

445+
public static final String GENERATE_UNMARSHAL_JSON = "generateUnmarshalJSON";
446+
public static final String GENERATE_UNMARSHAL_JSON_DESC = "Generate UnmarshalJSON method";
445447
public static final String MAX_ATTEMPTS_FOR_RETRY = "maxAttemptsForRetry";
446448

447449
public static final String WAIT_TIME_OF_THREAD = "waitTimeMillis";

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
4848
protected boolean generateInterfaces = false;
4949
protected boolean withGoMod = false;
5050
protected boolean generateMarshalJSON = true;
51+
protected boolean generateUnmarshalJSON = true;
52+
5153

5254
protected String packageName = "openapi";
5355
protected Set<String> numberTypes;
@@ -775,11 +777,11 @@ public ModelsMap postProcessModels(ModelsMap objs) {
775777
}
776778

777779
if (generateMarshalJSON) {
778-
model.vendorExtensions.put("x-go-generate-marshal-json", true);
780+
model.vendorExtensions.putIfAbsent("x-go-generate-marshal-json", true);
779781
}
780782

781-
if (!model.vendorExtensions.containsKey("x-go-generate-unmarshal-json")) {
782-
model.vendorExtensions.put("x-go-generate-unmarshal-json", true);
783+
if (generateUnmarshalJSON) {
784+
model.vendorExtensions.putIfAbsent("x-go-generate-unmarshal-json", true);
783785
}
784786
}
785787

@@ -936,6 +938,10 @@ public void setGenerateMarshalJSON(boolean generateMarshalJSON) {
936938
this.generateMarshalJSON = generateMarshalJSON;
937939
}
938940

941+
public void setGenerateUnmarshalJSON(boolean generateUnmarshalJSON) {
942+
this.generateUnmarshalJSON = generateUnmarshalJSON;
943+
}
944+
939945
@Override
940946
public String toDefaultValue(Schema schema) {
941947
schema = unaliasSchema(schema);

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public GoClientCodegen() {
150150
this.setDisallowAdditionalPropertiesIfNotPresent(true);
151151
cliOptions.add(CliOption.newBoolean(WITH_GO_MOD, "Generate go.mod and go.sum", true));
152152
cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_MARSHAL_JSON, CodegenConstants.GENERATE_MARSHAL_JSON_DESC, true));
153+
cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_UNMARSHAL_JSON, CodegenConstants.GENERATE_UNMARSHAL_JSON_DESC, true));
153154
this.setWithGoMod(true);
154155
}
155156

@@ -287,6 +288,11 @@ public void processOpts() {
287288
setGenerateMarshalJSON(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.GENERATE_MARSHAL_JSON).toString()));
288289
}
289290

291+
if (additionalProperties.containsKey(CodegenConstants.GENERATE_UNMARSHAL_JSON)) {
292+
setGenerateUnmarshalJSON(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.GENERATE_UNMARSHAL_JSON).toString()));
293+
}
294+
295+
290296
// add lambda for mustache templates to handle oneOf/anyOf naming
291297
// e.g. []string => ArrayOfString
292298
additionalProperties.put("lambda.type-to-name", (Mustache.Lambda) (fragment, writer) -> writer.write(typeToName(fragment.execute())));

modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ protected void verifyOptions() {
5353
verify(clientCodegen).setUseOneOfDiscriminatorLookup(GoClientOptionsProvider.USE_ONE_OF_DISCRIMINATOR_LOOKUP_VALUE);
5454
verify(clientCodegen).setWithGoMod(GoClientOptionsProvider.WITH_GO_MOD_VALUE);
5555
verify(clientCodegen).setGenerateMarshalJSON(GoClientOptionsProvider.GENERATE_MARSHAL_JSON_VALUE);
56+
verify(clientCodegen).setGenerateUnmarshalJSON(GoClientOptionsProvider.GENERATE_UNMARSHAL_JSON_VALUE);
5657
}
5758
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
3838
public static final boolean USE_ONE_OF_DISCRIMINATOR_LOOKUP_VALUE = true;
3939
public static final boolean WITH_GO_MOD_VALUE = true;
4040
public static final boolean GENERATE_MARSHAL_JSON_VALUE = true;
41+
public static final boolean GENERATE_UNMARSHAL_JSON_VALUE = true;
4142

4243
@Override
4344
public String getLanguage() {
@@ -60,6 +61,7 @@ public Map<String, String> createOptions() {
6061
.put(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, "true")
6162
.put(CodegenConstants.WITH_GO_MOD, "true")
6263
.put(CodegenConstants.GENERATE_MARSHAL_JSON, "true")
64+
.put(CodegenConstants.GENERATE_UNMARSHAL_JSON, "true")
6365
.put("generateInterfaces", "true")
6466
.put("structPrefix", "true")
6567
.build();

0 commit comments

Comments
 (0)