Skip to content
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

[ci] Sonar bug fixes #7497

Merged
merged 6 commits into from
Sep 24, 2020
Merged
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 @@ -394,19 +394,17 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
if (this.useOneOfInterfaces) {
// First, add newly created oneOf interfaces
for (CodegenModel cm : addOneOfInterfaces) {
Map<String, Object> modelValue = new HashMap<String, Object>() {{
putAll(additionalProperties());
put("model", cm);
}};
Map<String, Object> modelValue = new HashMap<>(additionalProperties());
modelValue.put("model", cm);

List<Object> modelsValue = Arrays.asList(modelValue);
List<Map<String, String>> importsValue = new ArrayList<Map<String, String>>();
Map<String, Object> objsValue = new HashMap<String, Object>() {{
put("models", modelsValue);
put("package", modelPackage());
put("imports", importsValue);
put("classname", cm.classname);
putAll(additionalProperties);
}};
Map<String, Object> objsValue = new HashMap<>();
objsValue.put("models", modelsValue);
objsValue.put("package", modelPackage());
objsValue.put("imports", importsValue);
objsValue.put("classname", cm.classname);
objsValue.putAll(additionalProperties);
objs.put(cm.name, objsValue);
}

Expand Down Expand Up @@ -2704,7 +2702,8 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) {
if (foundDisc != null) {
return foundDisc;
}
if (!!this.getLegacyDiscriminatorBehavior()) {

if (this.getLegacyDiscriminatorBehavior()) {
return null;
}
Discriminator disc = new Discriminator();
Expand Down Expand Up @@ -2742,7 +2741,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) {
throw new RuntimeException("The oneOf schemas have conflicting discriminator property names. " +
"oneOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames));
}
if ((hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getOneOf().size() && discriminatorsPropNames.size() == 1) {
if (foundDisc != null && (hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getOneOf().size() && discriminatorsPropNames.size() == 1) {
disc.setPropertyName(foundDisc.getPropertyName());
disc.setMapping(foundDisc.getMapping());
return disc;
Expand Down Expand Up @@ -2771,7 +2770,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) {
throw new RuntimeException("The anyOf schemas have conflicting discriminator property names. " +
"anyOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames));
}
if ((hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getAnyOf().size() && discriminatorsPropNames.size() == 1) {
if (foundDisc != null && (hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getAnyOf().size() && discriminatorsPropNames.size() == 1) {
disc.setPropertyName(foundDisc.getPropertyName());
disc.setMapping(foundDisc.getMapping());
return disc;
Expand Down Expand Up @@ -2860,8 +2859,12 @@ protected List<MappedModel> getAllOfDescendants(String thisSchemaName, OpenAPI o
List<MappedModel> descendentSchemas = new ArrayList();
Map<String, Schema> schemas = ModelUtils.getSchemas(openAPI);
String currentSchemaName = thisSchemaName;
while (true) {
for (String childName : schemas.keySet()) {
Set<String> keys = schemas.keySet();

int count = 0;
// hack: avoid infinite loop on potential self-references in event our checks fail.
while (100000 > count++) {
for (String childName : keys) {
if (childName.equals(thisSchemaName)) {
continue;
}
Expand All @@ -2879,8 +2882,8 @@ protected List<MappedModel> getAllOfDescendants(String thisSchemaName, OpenAPI o
continue;
}
String parentName = ModelUtils.getSimpleRef(ref);
if (parentName.equals(currentSchemaName)) {
if (queue.contains(childName) || descendentSchemas.contains(childName)) {
if (parentName != null && parentName.equals(currentSchemaName)) {
if (queue.contains(childName) || descendentSchemas.stream().anyMatch(i -> childName.equals(i.getMappingName()))) {
throw new RuntimeException("Stack overflow hit when looking for " + thisSchemaName + " an infinite loop starting and ending at " + childName + " was seen");
}
queue.add(childName);
Expand Down Expand Up @@ -3600,7 +3603,10 @@ protected void handleMethodResponse(Operation operation,
op.hasReference = schemas != null && schemas.containsKey(op.returnBaseType);

// lookup discriminator
Schema schema = schemas.get(op.returnBaseType);
Schema schema = null;
if (schemas != null) {
schema = schemas.get(op.returnBaseType);
}
if (schema != null) {
CodegenModel cmod = fromModel(op.returnBaseType, schema);
op.discriminator = cmod.discriminator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
@Override
public void processOpts() {
super.processOpts();
boolean isLibrary = false;

if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_PROJECT_GUID)) {
setPackageGuid((String) additionalProperties.get(CodegenConstants.OPTIONAL_PROJECT_GUID));
Expand All @@ -191,10 +190,6 @@ public void processOpts() {

additionalProperties.put(PROJECT_SDK, projectSdk);

// TODO - should we be supporting a Giraffe class library?
if (isLibrary)
LOGGER.warn("Library flag not currently supported.");

String authFolder = sourceFolder + File.separator + "auth";
String implFolder = sourceFolder + File.separator + "impl";
String helperFolder = sourceFolder + File.separator + "helpers";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1274,9 +1274,11 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
if (dataType == null && cm.isArrayModel) { // isAlias + arrayModelType missing "datatype"
dataType = "[" + cm.arrayModelType + "]";
}
cm.vendorExtensions.put(VENDOR_EXTENSION_X_DATA_TYPE, dataType);
if (dataType.equals("Maybe A.Value")) {
cm.vendorExtensions.put(VENDOR_EXTENSION_X_IS_MAYBE_VALUE, true);
if (dataType != null) {
cm.vendorExtensions.put(VENDOR_EXTENSION_X_DATA_TYPE, dataType);
if (dataType.equals("Maybe A.Value")) {
cm.vendorExtensions.put(VENDOR_EXTENSION_X_IS_MAYBE_VALUE, true);
}
}
}
for (CodegenProperty var : cm.vars) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public String toString() {
return f;
});

private static final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
private static final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000L;

private static final long MIN_DATE;

Expand Down Expand Up @@ -551,10 +551,13 @@ private void appendRandomByte(StringBuilder buffer, CodegenOperation op, Codegen
short inclusiveMax = (short) (var == null || !var.exclusiveMaximum ? 1 : 0);
byte randomByte = (byte) (min + exclusiveMin + ((max + inclusiveMax - min - exclusiveMin) * Math.random()));

if (loadTestDataFromFile)
var.addTestData(randomByte);
else
if (loadTestDataFromFile) {
if (var != null) {
var.addTestData(randomByte);
}
} else {
buffer.append(String.format(Locale.getDefault(), "(byte)%0#2x", randomByte));
}
}
}

Expand All @@ -568,10 +571,13 @@ private void appendRandomChar(StringBuilder buffer, CodegenOperation op, Codegen
char inclusiveMax = (char) (var == null || !var.exclusiveMaximum ? 1 : 0);
char randomChar = (char) (min + exclusiveMin + ((max + inclusiveMax - min - exclusiveMin) * Math.random()));

if (loadTestDataFromFile)
var.addTestData(randomChar);
else
if (loadTestDataFromFile) {
if (var != null) {
var.addTestData(randomChar);
}
} else {
buffer.append(String.format(Locale.getDefault(), "'%c'", randomChar));
}
}
}

Expand Down Expand Up @@ -607,10 +613,10 @@ private void appendRandomDate(StringBuilder buffer, CodegenOperation op, Codegen
BigDecimal exclusiveMinLong = new BigDecimal(var != null && var.exclusiveMinimum ? 1 : 0);
BigDecimal inclusiveMaxLong = new BigDecimal(var == null || !var.exclusiveMaximum ? 1 : 0);
long randomDateLong = minLong.add(exclusiveMinLong).add(maxLong.add(inclusiveMaxLong).subtract(minLong)
.subtract(exclusiveMinLong).multiply(new BigDecimal(Math.random()))).longValue();
.subtract(exclusiveMinLong).multiply(BigDecimal.valueOf(Math.random()))).longValue();

// If it's just a date without a time, round downwards to the nearest day.
if ("date".equals(var.dataFormat))
if (var != null && "date".equals(var.dataFormat))
randomDateLong = (randomDateLong % MILLIS_PER_DAY) * MILLIS_PER_DAY;

// NOTE: By default Jackson serializes Date as long milliseconds since epoch date, but that conflicts with
Expand All @@ -635,19 +641,20 @@ private void appendRandomDate(StringBuilder buffer, CodegenOperation op, Codegen
private void appendRandomDouble(StringBuilder buffer, CodegenOperation op, CodegenVariable var) {
if (!appendRandomEnum(buffer, op, var)) {
// NOTE: use BigDecimal to hold double values, to avoid numeric overflow.
BigDecimal min = new BigDecimal(
var == null || var.minimum == null ? Long.MIN_VALUE : Double.parseDouble(var.minimum));
BigDecimal max = new BigDecimal(
var == null || var.maximum == null ? Long.MAX_VALUE : Double.parseDouble(var.maximum));
BigDecimal min = BigDecimal.valueOf(var == null || var.minimum == null ? Long.MIN_VALUE : Double.parseDouble(var.minimum));
BigDecimal max = BigDecimal.valueOf(var == null || var.maximum == null ? Long.MAX_VALUE : Double.parseDouble(var.maximum));
BigDecimal exclusiveMin = new BigDecimal(var != null && var.exclusiveMinimum ? 1 : 0);
BigDecimal inclusiveMax = new BigDecimal(var == null || !var.exclusiveMaximum ? 1 : 0);
BigDecimal randomBigDecimal = min.add(exclusiveMin).add(max.add(inclusiveMax).subtract(min)
.subtract(exclusiveMin).multiply(new BigDecimal(String.valueOf(Math.random()))));

if (loadTestDataFromFile)
var.addTestData(randomBigDecimal);
else
if (loadTestDataFromFile) {
if (var != null) {
var.addTestData(randomBigDecimal);
}
} else {
buffer.append(randomBigDecimal.toString()).append('D');
}
}
}

Expand Down Expand Up @@ -704,10 +711,13 @@ private void appendRandomFloat(StringBuilder buffer, CodegenOperation op, Codege
float randomFloat = (float) (min + exclusiveMin
+ ((max + inclusiveMax - min - exclusiveMin) * Math.random()));

if (loadTestDataFromFile)
var.addTestData(randomFloat);
else
if (loadTestDataFromFile) {
if (var != null) {
var.addTestData(randomFloat);
}
} else {
buffer.append(String.format(Locale.getDefault(), "%g", randomFloat)).append('F');
}
}
}

Expand All @@ -720,10 +730,13 @@ private void appendRandomInt(StringBuilder buffer, CodegenOperation op, CodegenV
long inclusiveMax = var == null || !var.exclusiveMaximum ? 1 : 0;
int randomInt = (int) (min + exclusiveMin + ((max + inclusiveMax - min - exclusiveMin) * Math.random()));

if (loadTestDataFromFile)
var.addTestData(randomInt);
else
if (loadTestDataFromFile) {
if (var != null) {
var.addTestData(randomInt);
}
} else {
buffer.append(randomInt);
}
}
}

Expand All @@ -737,13 +750,16 @@ private void appendRandomLong(StringBuilder buffer, CodegenOperation op, Codegen
BigDecimal exclusiveMin = new BigDecimal(var != null && var.exclusiveMinimum ? 1 : 0);
BigDecimal inclusiveMax = new BigDecimal(var == null || !var.exclusiveMaximum ? 1 : 0);
long randomLong = min.add(exclusiveMin).add(
max.add(inclusiveMax).subtract(min).subtract(exclusiveMin).multiply(new BigDecimal(Math.random())))
max.add(inclusiveMax).subtract(min).subtract(exclusiveMin).multiply(BigDecimal.valueOf(Math.random())))
.longValue();

if (loadTestDataFromFile)
var.addTestData(randomLong);
else
if (loadTestDataFromFile) {
if (var != null) {
var.addTestData(randomLong);
}
} else {
buffer.append(randomLong).append('L');
}
}
}

Expand All @@ -757,10 +773,13 @@ private void appendRandomShort(StringBuilder buffer, CodegenOperation op, Codege
short randomShort = (short) (min + exclusiveMin
+ ((max + inclusiveMax - min - exclusiveMin) * Math.random()));

if (loadTestDataFromFile)
var.addTestData(randomShort);
else
if (loadTestDataFromFile) {
if (var != null) {
var.addTestData(randomShort);
}
} else {
buffer.append(String.format(Locale.getDefault(), "(short)%d", randomShort));
}
}
}

Expand All @@ -769,7 +788,9 @@ private void appendRandomString(StringBuilder buffer, CodegenOperation op, Codeg
String randomString = generateRandomString(var);

if (loadTestDataFromFile) {
var.addTestData(randomString);
if (var != null) {
var.addTestData(randomString);
}
} else {
buffer.append('"').append(randomString).append('"');
}
Expand Down Expand Up @@ -1375,13 +1396,15 @@ public void processOpts() {
break;
}
}
supportingFiles.remove(supportingFile);
SupportingFile updated = new SupportingFile(
supportingFile.getTemplateFile(),
supportingFile.getFolder(),
"ApplicationContext-" + invokerPackage + ".xml"
);
supportingFiles.add(updated);
if (supportingFile != null) {
supportingFiles.remove(supportingFile);
SupportingFile updated = new SupportingFile(
supportingFile.getTemplateFile(),
supportingFile.getFolder(),
"ApplicationContext-" + invokerPackage + ".xml"
);
supportingFiles.add(updated);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,11 +818,11 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
}

if (addImports) {
Map<String, String> imports2Classnames = new HashMap<String, String>() {{
put("JsonNullable", "org.openapitools.jackson.nullable.JsonNullable");
put("NoSuchElementException", "java.util.NoSuchElementException");
put("JsonIgnore", "com.fasterxml.jackson.annotation.JsonIgnore");
}};
Map<String, String> imports2Classnames = new HashMap<>();
imports2Classnames.put("JsonNullable", "org.openapitools.jackson.nullable.JsonNullable");
imports2Classnames.put("NoSuchElementException", "java.util.NoSuchElementException");
imports2Classnames.put("JsonIgnore", "com.fasterxml.jackson.annotation.JsonIgnore");

for (Map.Entry<String, String> entry : imports2Classnames.entrySet()) {
cm.imports.add(entry.getKey());
Map<String, String> importsItem = new HashMap<String, String>();
Expand Down Expand Up @@ -976,9 +976,9 @@ public String toApiVarName(String name) {
@Override
public void addImportsToOneOfInterface(List<Map<String, String>> imports) {
for (String i : Arrays.asList("JsonSubTypes", "JsonTypeInfo")) {
Map<String, String> oneImport = new HashMap<String, String>() {{
put("import", importMapping.get(i));
}};
Map<String, String> oneImport = new HashMap<>();
oneImport.put("import", importMapping.get(i));

if (!imports.contains(oneImport)) {
imports.add(oneImport);
}
Expand Down
Loading