[BUG][JAVA] Handling default values leads to wrong defaults or not compilable code fixed (#15836)

* (fix): default values for array types will be handled a bit more appropriate

* (fix): remove commented out block
This commit is contained in:
JanLubenow
2023-06-14 05:18:11 +02:00
committed by GitHub
parent 6a1626c8e7
commit 175876a105
6 changed files with 125 additions and 1 deletions

View File

@@ -974,6 +974,12 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
_default.elements().forEachRemaining((element) -> {
final_values.add(element.asText());
});
} else if (schema.getDefault() instanceof Collection) {
var _default = (Collection<String>) schema.getDefault();
List<String> final_values = _values;
_default.forEach((element) -> {
final_values.add(element);
});
} else { // single value
_values = java.util.Collections.singletonList(String.valueOf(schema.getDefault()));
}
@@ -986,7 +992,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
defaultValues.add(cp.items.datatypeWithEnum + "." + toEnumVarName(_value, cp.items.dataType));
}
defaultValue = StringUtils.join(defaultValues, ", ");
} else {
} else if (_values.size() > 0) {
if (cp.items.isString) { // array item is string
defaultValue = String.format(Locale.ROOT, "\"%s\"", StringUtils.join(_values, "\", \""));
} else if (cp.items.isNumeric) {
@@ -1006,7 +1012,10 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
} else { // array item is non-string, e.g. integer
defaultValue = StringUtils.join(_values, ", ");
}
} else {
return "new ArrayList<>()";
}
return String.format(Locale.ROOT, "new ArrayList<>(Arrays.asList(%s))", defaultValue);
} else if (cp.isArray && cp.getUniqueItems()) { // set
// TODO

View File

@@ -1617,6 +1617,37 @@ public class JavaClientCodegenTest {
.containsWithNameAndAttributes("JsonbProperty", ImmutableMap.of("value", "\"c\""));
}
@Test
public void testJavaClientDefaultValues_issueNoNumber() throws Exception {
Map<String, Object> properties = new HashMap<>();
properties.put(JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "3.0");
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setAdditionalProperties(properties)
.setGeneratorName("java")
.setLibrary(JavaClientCodegen.WEBCLIENT)
.setInputSpec("src/test/resources/bugs/java-codegen-empty-array-as-default-value/issue_wrong-default.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
final ClientOptInput clientOptInput = configurator.toClientOptInput();
DefaultGenerator generator = new DefaultGenerator();
Map<String, File> files = generator.opts(clientOptInput).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));
JavaFileAssert.assertThat(files.get("DefaultValuesType.java"))
.hasProperty("stringDefault")
.asString().endsWith("= new ArrayList<>();");
JavaFileAssert.assertThat(files.get("DefaultValuesType.java"))
.hasProperty("stringDefault2")
.asString().endsWith("= new ArrayList<>(Arrays.asList(\"Hallo\", \"Huhu\"));");
JavaFileAssert.assertThat(files.get("DefaultValuesType.java"))
.hasProperty("objectDefault")
.asString().endsWith("= new ArrayList<>();");
}
@Test
public void testWebClientJsonCreatorWithNullable_issue12790() throws Exception {
Map<String, Object> properties = new HashMap<>();

View File

@@ -0,0 +1,10 @@
$schema: https://json-schema.org/draft/2020-12/schema
title: BaseType
type: object
required:
- property
properties:
property1:
type: array
items:
$ref: 'defaultValuesType.yaml'

View File

@@ -0,0 +1,23 @@
$schema: https://json-schema.org/draft/2020-12/schema
title: DefaultValuesType
type: object
properties:
stringDefault:
type: array
default: []
items:
type: string
stringDefault2:
type: array
default:
- "Hallo"
- "Huhu"
items:
type: string
objectDefault:
type: array
default: []
items:
$ref: 'objectType.yaml'

View File

@@ -0,0 +1,45 @@
openapi: 3.0.3
info:
version: 1.0.0
title: Subfile-Defaults
paths:
"/dummy":
post:
operationId: doSomething
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/BaseRequest'
responses:
"200":
description: Successful Operation.
content:
application/json:
schema:
$ref: '#/components/schemas/BaseResponse'
components:
schemas:
BaseRequest:
type: object
required:
- data
properties:
data:
$ref: './baseType.yaml'
BaseResponse:
type: object
properties:
status:
$ref: '#/components/schemas/Status'
Status:
type: string
enum:
- GOOD
- BAD

View File

@@ -0,0 +1,6 @@
$schema: https://json-schema.org/draft/2020-12/schema
title: ObjectType
type: object
properties:
someInt:
type: integer