Skip to content

Commit cd17738

Browse files
committed
[Java] Fix default values of array-type parameters in a referenced file (OpenAPITools#17779)
* fix invalid default values of parameters with array type in a referenced file * add test
1 parent b8bd779 commit cd17738

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

+8
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,9 @@ public String toDefaultParameterValue(final Schema<?> schema) {
12891289
return localDate.toString();
12901290
}
12911291
if (ModelUtils.isArraySchema(schema)) {
1292+
// swagger-parser parses the default value differently depending on whether it's in a referenced file or not.
1293+
// cf. https://github.com/swagger-api/swagger-parser/issues/1958
1294+
// ArrayList if in the referenced file, ArrayNode if not.
12921295
if (defaultValue instanceof ArrayNode) {
12931296
ArrayNode array = (ArrayNode) defaultValue;
12941297
return StreamSupport.stream(array.spliterator(), false)
@@ -1297,6 +1300,11 @@ public String toDefaultParameterValue(final Schema<?> schema) {
12971300
.map(item -> StringUtils.removeStart(item, "\""))
12981301
.map(item -> StringUtils.removeEnd(item, "\""))
12991302
.collect(Collectors.joining(","));
1303+
} else if (defaultValue instanceof ArrayList) {
1304+
ArrayList<?> array = (ArrayList<?>) defaultValue;
1305+
return array.stream()
1306+
.map(Object::toString)
1307+
.collect(Collectors.joining(","));
13001308
}
13011309
}
13021310
// escape quotes

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@
1717

1818
package org.openapitools.codegen.java;
1919

20+
import io.swagger.parser.OpenAPIParser;
2021
import io.swagger.v3.oas.models.Components;
2122
import io.swagger.v3.oas.models.OpenAPI;
2223
import io.swagger.v3.oas.models.media.*;
2324

25+
import io.swagger.v3.oas.models.parameters.Parameter;
26+
import io.swagger.v3.parser.core.models.ParseOptions;
2427
import java.time.OffsetDateTime;
2528
import java.time.ZonedDateTime;
2629
import java.util.*;
2730

31+
import java.util.stream.Collectors;
2832
import org.openapitools.codegen.*;
2933
import org.openapitools.codegen.languages.AbstractJavaCodegen;
3034
import org.openapitools.codegen.utils.ModelUtils;
@@ -873,6 +877,26 @@ public void testOneOfModelImports() throws Exception {
873877
Assert.assertTrue(cm.imports.contains("UUID"));
874878
}
875879

880+
@Test
881+
public void arrayParameterDefaultValueDoesNotNeedBraces() throws Exception {
882+
ParseOptions parseOptions = new ParseOptions();
883+
parseOptions.setResolve(true);
884+
final OpenAPI openAPI = new OpenAPIParser()
885+
.readLocation("src/test/resources/3_0/issue_16223.yaml", null, parseOptions)
886+
.getOpenAPI();
887+
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
888+
codegen.setOpenAPI(openAPI);
889+
890+
Map<String, Schema> schemas = openAPI.getPaths().get("/test").getGet().getParameters().stream()
891+
.collect(Collectors.toMap(
892+
Parameter::getName,
893+
p -> ModelUtils.getReferencedSchema(openAPI, p.getSchema())));
894+
Assert.assertEquals(codegen.toDefaultParameterValue(schemas.get("fileEnumWithDefault")), "A,B");
895+
Assert.assertEquals(codegen.toDefaultParameterValue(schemas.get("fileEnumWithDefaultEmpty")), "");
896+
Assert.assertEquals(codegen.toDefaultParameterValue(schemas.get("inlineEnumWithDefault")), "A,B");
897+
Assert.assertEquals(codegen.toDefaultParameterValue(schemas.get("inlineEnumWithDefaultEmpty")), "");
898+
}
899+
876900
private static Schema<?> createObjectSchemaWithMinItems() {
877901
return new ObjectSchema()
878902
.addProperties("id", new IntegerSchema().format("int32"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
openapi: 3.0.3
3+
info:
4+
title: Test
5+
version: 1.0.0-SNAPSHOT
6+
paths:
7+
/test:
8+
get:
9+
parameters:
10+
- name: fileEnumWithDefault
11+
in: query
12+
schema:
13+
$ref: './issue_16223_enum_with_default.yaml'
14+
- name: fileEnumWithDefaultEmpty
15+
in: query
16+
schema:
17+
$ref: './issue_16223_enum_with_default_empty.yaml'
18+
- name: inlineEnumWithDefault
19+
in: query
20+
schema:
21+
type: array
22+
items:
23+
type: string
24+
enum:
25+
- A
26+
- B
27+
- C
28+
default:
29+
- A
30+
- B
31+
- name: inlineEnumWithDefaultEmpty
32+
in: query
33+
schema:
34+
type: array
35+
items:
36+
type: string
37+
enum:
38+
- A
39+
- B
40+
- C
41+
default: []
42+
responses:
43+
"200":
44+
description: OK
45+
46+
components:
47+
schemas:
48+
Test:
49+
type: object
50+
properties:
51+
withDefault:
52+
$ref: './issue_16223_enum_with_default.yaml'
53+
withEmptyDefault:
54+
$ref: './issue_16223_enum_with_default_empty.yaml'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type: array
2+
items:
3+
type: string
4+
enum:
5+
- A
6+
- B
7+
- C
8+
default:
9+
- A
10+
- B
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type: array
2+
items:
3+
type: string
4+
enum:
5+
- A
6+
- B
7+
- C
8+
default: []

0 commit comments

Comments
 (0)