From 4616ff3e4636fcf79e704a01327a8da56b9328c6 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sun, 21 Apr 2024 22:30:20 +0800 Subject: [PATCH] add generate unmarshal json option in go client generator --- .../org/openapitools/codegen/CodegenConstants.java | 2 ++ .../codegen/languages/AbstractGoCodegen.java | 12 +++++++++--- .../codegen/languages/GoClientCodegen.java | 6 ++++++ .../openapitools/codegen/go/GoClientOptionsTest.java | 1 + .../codegen/options/GoClientOptionsProvider.java | 2 ++ 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java index 84920eacfd35..017bf79406b0 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java @@ -442,6 +442,8 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case, public static final String GENERATE_MARSHAL_JSON = "generateMarshalJSON"; public static final String GENERATE_MARSHAL_JSON_DESC = "Generate MarshalJSON method"; + public static final String GENERATE_UNMARSHAL_JSON = "generateUnmarshalJSON"; + public static final String GENERATE_UNMARSHAL_JSON_DESC = "Generate UnmarshalJSON method"; public static final String MAX_ATTEMPTS_FOR_RETRY = "maxAttemptsForRetry"; public static final String WAIT_TIME_OF_THREAD = "waitTimeMillis"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java index 3f0932b845ec..ed02f152fa19 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java @@ -48,6 +48,8 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege protected boolean generateInterfaces = false; protected boolean withGoMod = false; protected boolean generateMarshalJSON = true; + protected boolean generateUnmarshalJSON = true; + protected String packageName = "openapi"; protected Set numberTypes; @@ -775,11 +777,11 @@ public ModelsMap postProcessModels(ModelsMap objs) { } if (generateMarshalJSON) { - model.vendorExtensions.put("x-go-generate-marshal-json", true); + model.vendorExtensions.putIfAbsent("x-go-generate-marshal-json", true); } - if (!model.vendorExtensions.containsKey("x-go-generate-unmarshal-json")) { - model.vendorExtensions.put("x-go-generate-unmarshal-json", true); + if (generateUnmarshalJSON) { + model.vendorExtensions.putIfAbsent("x-go-generate-unmarshal-json", true); } } @@ -936,6 +938,10 @@ public void setGenerateMarshalJSON(boolean generateMarshalJSON) { this.generateMarshalJSON = generateMarshalJSON; } + public void setGenerateUnmarshalJSON(boolean generateUnmarshalJSON) { + this.generateUnmarshalJSON = generateUnmarshalJSON; + } + @Override public String toDefaultValue(Schema schema) { schema = unaliasSchema(schema); diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java index f079b4507867..060dfc277d73 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java @@ -150,6 +150,7 @@ public GoClientCodegen() { 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)); + cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_UNMARSHAL_JSON, CodegenConstants.GENERATE_UNMARSHAL_JSON_DESC, true)); this.setWithGoMod(true); } @@ -287,6 +288,11 @@ public void processOpts() { setGenerateMarshalJSON(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.GENERATE_MARSHAL_JSON).toString())); } + if (additionalProperties.containsKey(CodegenConstants.GENERATE_UNMARSHAL_JSON)) { + setGenerateUnmarshalJSON(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.GENERATE_UNMARSHAL_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()))); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java index 8a67ee0e68eb..603515e7e9cd 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java @@ -53,5 +53,6 @@ protected void verifyOptions() { 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); + verify(clientCodegen).setGenerateUnmarshalJSON(GoClientOptionsProvider.GENERATE_UNMARSHAL_JSON_VALUE); } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java index 830fd29a6c54..372e293f636d 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java @@ -38,6 +38,7 @@ public class GoClientOptionsProvider implements OptionsProvider { 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; + public static final boolean GENERATE_UNMARSHAL_JSON_VALUE = true; @Override public String getLanguage() { @@ -60,6 +61,7 @@ public Map createOptions() { .put(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, "true") .put(CodegenConstants.WITH_GO_MOD, "true") .put(CodegenConstants.GENERATE_MARSHAL_JSON, "true") + .put(CodegenConstants.GENERATE_UNMARSHAL_JSON, "true") .put("generateInterfaces", "true") .put("structPrefix", "true") .build();