Skip to content

Commit

Permalink
add generate unmarshal json option in go client generator
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Apr 21, 2024
1 parent ba95e7b commit 4616ff3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> numberTypes;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -60,6 +61,7 @@ public Map<String, String> 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();
Expand Down

0 comments on commit 4616ff3

Please sign in to comment.