[Java] Fixes Spring generator devaultValues for int64/float/double (#4969)

* Updates spring generator to omit type suffixes from int float double defaults, adds testDefaultValuesFixed

Adds the test testDefaultValuesFixed

* Updates SpringCodegen to only remove character suffixes from CodegenParameter defaultValues, updates tests

* Updates java function comment

* Adds early return in postProcessParameter

* Removes unneeded imports

* Fixes decorators on java method postProcessParameter

* Fixes typo

* Fixes paste error

* Removes unused import
This commit is contained in:
Justin Black 2020-01-14 06:38:16 -08:00 committed by William Cheng
parent 4767259df4
commit 8a94a3a7d5
3 changed files with 136 additions and 6 deletions

View File

@ -877,4 +877,25 @@ public class SpringCodegen extends AbstractJavaCodegen
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);
}
}
}

View File

@ -19,13 +19,12 @@ package org.openapitools.codegen.java.spring;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.MockDefaultGenerator;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.SpringCodegen;
import org.openapitools.codegen.languages.features.CXFServerFeatures;
import org.testng.Assert;
@ -440,7 +439,7 @@ public class SpringCodegenTest {
checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/ExampleApi.java",
"@RequestBody(required = false");
}
@Test
public void useBeanValidationTruePerformBeanValidationTrueJava8FalseForFormatEmail() throws IOException {
beanValidationForFormatEmail(true, true, false, "@org.hibernate.validator.constraints.Email", "@javax.validation.constraints.Email");
@ -455,7 +454,7 @@ public class SpringCodegenTest {
public void useBeanValidationTruePerformBeanValidationTrueJava8TrueForFormatEmail() throws IOException {
beanValidationForFormatEmail(true, true, true, "@javax.validation.constraints.Email", "@org.hibernate.validator.constraints.Email");
}
private void beanValidationForFormatEmail(boolean useBeanValidation, boolean performBeanValidation, boolean java8, String contains, String notContains) throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
@ -481,4 +480,43 @@ public class SpringCodegenTest {
checkFileNotContains(generator, outputPath + "/src/main/java/org/openapitools/model/PersonWithEmail.java", notContains);
}
@Test
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/2_0/issue1226.yaml");
final SpringCodegen codegen = new SpringCodegen();
codegen.setOpenAPI(openAPI);
String int64Val = "9223372036854775807l";
String floatVal = "3.14159f";
String doubleVal = "3.14159d";
// make sure that the model properties include character suffixes
String modelName = "NumberHolder";
Schema nhSchema = openAPI.getComponents().getSchemas().get(modelName);
CodegenModel cm = codegen.fromModel(modelName, nhSchema);
CodegenProperty int64Prop = cm.vars.get(0);
CodegenProperty floatProp = cm.vars.get(1);
CodegenProperty doubleProp = cm.vars.get(2);
Assert.assertEquals(int64Prop.defaultValue, int64Val);
Assert.assertEquals(floatProp.defaultValue, floatVal);
Assert.assertEquals(doubleProp.defaultValue, doubleVal);
int64Val = "9223372036854775807";
floatVal = "3.14159";
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);
}
}

View File

@ -0,0 +1,71 @@
swagger: '2.0'
info:
description: 'blah'
version: 1.0.0
title: sample spec
host: fake.site.com
tags: []
schemes:
- https
paths:
/numberdata:
post:
summary: Get back a NumberHolder
description: ''
operationId: getNumberHolder
consumes:
- application/json
produces:
- application/json
parameters: []
responses:
'200':
description: successful operation
schema:
$ref: '#/definitions/NumberHolder'
/numericqueryparams:
get:
tags:
- user
summary: a test route for numeric query params
description: ''
operationId: numericQueryParams
produces:
- application/json
parameters:
- in: query
name: int64
type: integer
format: int64
default: 9223372036854775807
- in: query
name: float
type: number
format: float
default: 3.14159
- in: query
name: double
type: number
format: double
default: 3.14159
responses:
default:
description: successful operation
securityDefinitions: {}
definitions:
NumberHolder:
description: A model to hold the three number types which should no longer have string suffixes
type: object
properties:
int64:
type: integer
format: int64
default: 9223372036854775807
float:
type: number
format: float
default: 3.14159
double:
type: number
format: double
default: 3.14159