Skip to content

Sanitize additionalProperties with CliOptions #7746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5550,8 +5550,12 @@ public void setIgnoreFilePathOverride(final String ignoreFileOverride) {
}

public boolean convertPropertyToBoolean(String propertyKey) {
final Object booleanValue = additionalProperties.get(propertyKey);
Boolean result = Boolean.FALSE;
final Object value = additionalProperties.get(propertyKey);
return toBoolean(value);
}

protected boolean toBoolean(Object booleanValue) {
boolean result = false;
if (booleanValue instanceof Boolean) {
result = (Boolean) booleanValue;
} else if (booleanValue instanceof String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static java.util.Arrays.asList;
import static org.openapitools.codegen.utils.StringUtils.*;

public abstract class AbstractJavaCodegen extends DefaultCodegen implements CodegenConfig {
Expand Down Expand Up @@ -159,7 +160,7 @@ public AbstractJavaCodegen() {
"native", "super", "while", "null")
);

languageSpecificPrimitives = new HashSet<String>(
languageSpecificPrimitives = new HashSet<>(
Arrays.asList(
"String",
"boolean",
Expand Down Expand Up @@ -229,17 +230,98 @@ public AbstractJavaCodegen() {
cliOptions.add(CliOption.newString(CodegenConstants.PARENT_GROUP_ID, CodegenConstants.PARENT_GROUP_ID_DESC));
cliOptions.add(CliOption.newString(CodegenConstants.PARENT_ARTIFACT_ID, CodegenConstants.PARENT_ARTIFACT_ID_DESC));
cliOptions.add(CliOption.newString(CodegenConstants.PARENT_VERSION, CodegenConstants.PARENT_VERSION_DESC));
CliOption snapShotVersion = CliOption.newString(CodegenConstants.SNAPSHOT_VERSION, CodegenConstants.SNAPSHOT_VERSION_DESC);
CliOption snapShotVersion = CliOption.newBoolean(CodegenConstants.SNAPSHOT_VERSION, CodegenConstants.SNAPSHOT_VERSION_DESC);
Map<String, String> snapShotVersionOptions = new HashMap<>();
snapShotVersionOptions.put("true", "Use a SnapShot Version");
snapShotVersionOptions.put("false", "Use a Release Version");
snapShotVersion.setEnum(snapShotVersionOptions);
cliOptions.add(snapShotVersion);

cliOptions.add(CliOption.newBoolean(SUPPORT_ASYNC, "", (boolean) defaultValue(SUPPORT_ASYNC)));
}

protected Object defaultValue(String key) {
switch (key) {
case CodegenConstants.MODEL_PACKAGE:
return modelPackage;
case CodegenConstants.API_PACKAGE:
return apiPackage;
case CodegenConstants.INVOKER_PACKAGE:
return this.getInvokerPackage();
case CodegenConstants.GROUP_ID:
return this.getGroupId();
case CodegenConstants.ARTIFACT_ID:
return this.getArtifactId();
case CodegenConstants.ARTIFACT_VERSION:
return ARTIFACT_VERSION_DEFAULT_VALUE;
case CodegenConstants.ARTIFACT_URL:
return this.getArtifactUrl();
case CodegenConstants.ARTIFACT_DESCRIPTION:
return this.getArtifactDescription();
case CodegenConstants.SCM_CONNECTION:
return this.getScmConnection();
case CodegenConstants.SCM_DEVELOPER_CONNECTION:
return this.getScmDeveloperConnection();
case CodegenConstants.SCM_URL:
return this.getScmUrl();
case CodegenConstants.DEVELOPER_NAME:
return this.getDeveloperName();
case CodegenConstants.DEVELOPER_EMAIL:
return this.getDeveloperEmail();
case CodegenConstants.DEVELOPER_ORGANIZATION:
return this.getDeveloperOrganization();
case CodegenConstants.DEVELOPER_ORGANIZATION_URL:
return this.getDeveloperOrganizationUrl();
case CodegenConstants.LICENSE_NAME:
return this.getLicenseName();
case CodegenConstants.LICENSE_URL:
return this.getLicenseUrl();
case CodegenConstants.SOURCE_FOLDER:
return this.getSourceFolder();
case CodegenConstants.SERIALIZABLE_MODEL:
return this.getSerializableModel();
case CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING:
return serializeBigDecimalAsString;
case FULL_JAVA_UTIL:
return fullJavaUtil;
case DISCRIMINATOR_CASE_SENSITIVE:
return discriminatorCaseSensitive;
case CodegenConstants.HIDE_GENERATION_TIMESTAMP:
return this.isHideGenerationTimestamp();
case WITH_XML:
return withXml;
case DATE_LIBRARY:
return this.getDateLibrary();
case JAVA8_MODE:
return this.java8Mode;
case DISABLE_HTML_ESCAPING:
return disableHtmlEscaping;
case BOOLEAN_GETTER_PREFIX:
return this.getBooleanGetterPrefix();
case IGNORE_ANYOF_IN_ENUM:
return ignoreAnyOfInEnum;
case ADDITIONAL_MODEL_TYPE_ANNOTATIONS:
return additionalModelTypeAnnotations;
case OPENAPI_NULLABLE:
return this.openApiNullable;

case CodegenConstants.PARENT_GROUP_ID:
return parentGroupId;
case CodegenConstants.PARENT_ARTIFACT_ID:
return parentArtifactId;
case CodegenConstants.PARENT_VERSION:
return parentVersion;
case CodegenConstants.SNAPSHOT_VERSION:
return false;
case SUPPORT_ASYNC:
return supportAsync;
}
return null;
}

@Override
public void processOpts() {
coerceBooleanAdditionalProperties();
super.processOpts();

if (StringUtils.isEmpty(System.getenv("JAVA_POST_PROCESS_FILE"))) {
Expand Down Expand Up @@ -504,26 +586,15 @@ public void processOpts() {
importMapping.put("com.fasterxml.jackson.annotation.JsonProperty", "com.fasterxml.jackson.annotation.JsonCreator");

if (additionalProperties.containsKey(JAVA8_MODE)) {
setJava8Mode(Boolean.parseBoolean(additionalProperties.get(JAVA8_MODE).toString()));
if (java8Mode) {
additionalProperties.put("java8", true);
} else {
additionalProperties.put("java8", false);
}
setJava8Mode((boolean) additionalProperties.get(JAVA8_MODE));
}

if (additionalProperties.containsKey(SUPPORT_ASYNC)) {
setSupportAsync(Boolean.parseBoolean(additionalProperties.get(SUPPORT_ASYNC).toString()));
if (supportAsync) {
additionalProperties.put(SUPPORT_ASYNC, "true");
}
setSupportAsync((boolean) additionalProperties.get(SUPPORT_ASYNC));
}

if (additionalProperties.containsKey(WITH_XML)) {
setWithXml(Boolean.parseBoolean(additionalProperties.get(WITH_XML).toString()));
if (withXml) {
additionalProperties.put(WITH_XML, "true");
}
setWithXml((boolean) additionalProperties.get(WITH_XML));
}

if (additionalProperties.containsKey(DATE_LIBRARY)) {
Expand All @@ -544,7 +615,7 @@ public void processOpts() {
importMapping.put("LocalDate", "org.joda.time.LocalDate");
importMapping.put("DateTime", "org.joda.time.DateTime");
} else if (dateLibrary.startsWith("java8")) {
additionalProperties.put("java8", "true");
additionalProperties.put("java8", true);
additionalProperties.put("jsr310", "true");
typeMapping.put("date", "LocalDate");
importMapping.put("LocalDate", "java.time.LocalDate");
Expand Down Expand Up @@ -888,7 +959,7 @@ public String toDefaultValue(Schema schema) {
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return String.format(Locale.ROOT, localDate.toString(), "");
} else if (schema.getDefault() instanceof java.time.OffsetDateTime) {
return "OffsetDateTime.parse(\"" + String.format(Locale.ROOT, ((java.time.OffsetDateTime) schema.getDefault()).atZoneSameInstant(ZoneId.systemDefault()).toString(), "") + "\", java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(java.time.ZoneId.systemDefault()))";
return "OffsetDateTime.parse(\"" + String.format(Locale.ROOT, ((java.time.OffsetDateTime) schema.getDefault()).atZoneSameInstant(ZoneId.systemDefault()).toString(), "") + "\", java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(java.time.ZoneId.systemDefault()))";
} else {
_default = (String) schema.getDefault();
}
Expand Down Expand Up @@ -1166,7 +1237,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);

if (additionalProperties.containsKey(CodegenConstants.SNAPSHOT_VERSION)) {
if (convertPropertyToBooleanAndWriteBack(CodegenConstants.SNAPSHOT_VERSION)) {
if (convertPropertyToBoolean(CodegenConstants.SNAPSHOT_VERSION)) {
this.setArtifactVersion(this.buildSnapshotVersion(this.getArtifactVersion()));
}
}
Expand Down Expand Up @@ -1727,4 +1798,31 @@ protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Sc
addImport(codegenModel, codegenModel.additionalPropertiesType);
}
}

protected Set<String> booleanAdditionalProperties() {
return new HashSet<>(asList(
CodegenConstants.SERIALIZABLE_MODEL,
CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING,
FULL_JAVA_UTIL,
DISCRIMINATOR_CASE_SENSITIVE,
CodegenConstants.HIDE_GENERATION_TIMESTAMP,
WITH_XML,
JAVA8_MODE,
DISABLE_HTML_ESCAPING,
IGNORE_ANYOF_IN_ENUM,
OPENAPI_NULLABLE,
CodegenConstants.SNAPSHOT_VERSION,
SUPPORT_ASYNC
));
}

protected void coerceBooleanAdditionalProperties() {
booleanAdditionalProperties().forEach(prop -> {
boolean defaultValue = (boolean) defaultValue(prop);
Object value = additionalProperties.getOrDefault(prop, defaultValue);
if (value != null) {
additionalProperties.put(prop, toBoolean(value));
}
});
}
}
Loading