Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,27 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
return op;
}

@Override
public void postProcessParameter(CodegenParameter p) {
// we use a custom version of this function to remove the l, d, and f suffixes from Long/Double/Float
// defaultValues
// remove the l because our users will use Long.parseLong(String defaultValue)
// remove the d because our users will use Double.parseDouble(String defaultValue)
// remove the f because our users will use Float.parseFloat(String defaultValue)
// NOTE: for CodegenParameters we DO need these suffixes because those defaultValues are used as java value
// literals assigned to Long/Double/Float
if (p.defaultValue == null) {
return;
}

Boolean fixLong = (p.isLong && "l".equals(p.defaultValue.substring(p.defaultValue.length()-1)));
Boolean fixDouble = (p.isDouble && "d".equals(p.defaultValue.substring(p.defaultValue.length()-1)));
Boolean fixFloat = (p.isFloat && "f".equals(p.defaultValue.substring(p.defaultValue.length()-1)));
if (fixLong || fixDouble || fixFloat) {
p.defaultValue = p.defaultValue.substring(0, p.defaultValue.length()-1);
}
}

private static CodegenModel reconcileInlineEnums(CodegenModel codegenModel, CodegenModel parentCodegenModel) {
// This generator uses inline classes to define enums, which breaks when
// dealing with models that have subTypes. To clean this up, we will analyze
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@

package org.openapitools.codegen.languages;

import io.swagger.v3.oas.models.Operation;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.features.JbossFeature;
import org.openapitools.codegen.meta.features.DocumentationFeature;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,25 +874,4 @@ public void setPerformBeanValidation(boolean performBeanValidation) {
public void setUseOptional(boolean useOptional) {
this.useOptional = useOptional;
}

@Override
public void postProcessParameter(CodegenParameter p) {
// we use a custom version of this function to remove the l, d, and f suffixes from Long/Double/Float
// defaultValues
// remove the l because our users will use Long.parseLong(String defaultValue)
// remove the d because our users will use Double.parseDouble(String defaultValue)
// remove the f because our users will use Float.parseFloat(String defaultValue)
// NOTE: for CodegenParameters we DO need these suffixes because those defaultValues are used as java value
// literals assigned to Long/Double/Float
if (p.defaultValue == null) {
return;
}
Boolean fixLong = (p.isLong && "l".equals(p.defaultValue.substring(p.defaultValue.length()-1)));
Boolean fixDouble = (p.isDouble && "d".equals(p.defaultValue.substring(p.defaultValue.length()-1)));
Boolean fixFloat = (p.isFloat && "f".equals(p.defaultValue.substring(p.defaultValue.length()-1)));
if (fixLong || fixDouble || fixFloat) {
p.defaultValue = p.defaultValue.substring(0, p.defaultValue.length()-1);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.openapitools.codegen.java.jaxrs;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.Schema;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.JavaResteasyServerCodegen;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -33,4 +38,27 @@ public void mapModelTest() {
assertTrue(cm.imports.contains("HashMap"));
}

@Test(description = "remove suffix for int64, float and double types")
public void testDefaultValuesFixed() {
// we had an issue where int64, float, and double values were having single character string suffixes
// included in their defaultValues
// This test verifies that those characters are no longer present
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/issue8986.yaml");
final JavaResteasyServerCodegen codegen = new JavaResteasyServerCodegen();
codegen.setOpenAPI(openAPI);

String int64Val = "100";
String floatVal = "3.14159";
String doubleVal = "3.14159";
// make sure that the operation parameters omit character suffixes.
String route = "/numericqueryparams";
Operation op = openAPI.getPaths().get(route).getGet();
CodegenOperation co = codegen.fromOperation(route, "GET", op, null);
CodegenParameter int64Param = co.queryParams.get(0);
CodegenParameter floatParam = co.queryParams.get(1);
CodegenParameter doubleParam = co.queryParams.get(2);
Assert.assertEquals(int64Param.defaultValue, int64Val);
Assert.assertEquals(floatParam.defaultValue, floatVal);
Assert.assertEquals(doubleParam.defaultValue, doubleVal);
}
}
40 changes: 40 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue8986.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
openapi: 3.0.0
info:
title: My title
description: API under test
version: 1.0.7
servers:
- url: https://localhost:9999/root
paths:
/numericqueryparams:
get:
tags:
- user
summary: a test route for numeric query params
description: ''
operationId: numericQueryParams
produces:
- application/json
parameters:
- in: query
name: int64
schema:
type: integer
format: int64
default: 100
- in: query
name: float
schema:
type: number
format: float
default: 3.14159
- in: query
name: double
schema:
type: number
format: double
default: 3.14159
responses:
default:
description: successful operation
securityDefinitions: {}