[Java] Fix default values of array-type parameters in a referenced file (#17779)

* fix invalid default values of parameters with array type in a referenced file

* add test
This commit is contained in:
Tomohiko Ozawa 2024-02-07 13:46:43 +09:00 committed by GitHub
parent d0ed25a06d
commit 795f0798ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 104 additions and 0 deletions

View File

@ -1289,6 +1289,9 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
return localDate.toString();
}
if (ModelUtils.isArraySchema(schema)) {
// swagger-parser parses the default value differently depending on whether it's in a referenced file or not.
// cf. https://github.com/swagger-api/swagger-parser/issues/1958
// ArrayList if in the referenced file, ArrayNode if not.
if (defaultValue instanceof ArrayNode) {
ArrayNode array = (ArrayNode) defaultValue;
return StreamSupport.stream(array.spliterator(), false)
@ -1297,6 +1300,11 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
.map(item -> StringUtils.removeStart(item, "\""))
.map(item -> StringUtils.removeEnd(item, "\""))
.collect(Collectors.joining(","));
} else if (defaultValue instanceof ArrayList) {
ArrayList<?> array = (ArrayList<?>) defaultValue;
return array.stream()
.map(Object::toString)
.collect(Collectors.joining(","));
}
}
// escape quotes

View File

@ -17,14 +17,18 @@
package org.openapitools.codegen.java;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.parser.core.models.ParseOptions;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Collectors;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.AbstractJavaCodegen;
import org.openapitools.codegen.utils.ModelUtils;
@ -873,6 +877,26 @@ public class AbstractJavaCodegenTest {
Assert.assertTrue(cm.imports.contains("UUID"));
}
@Test
public void arrayParameterDefaultValueDoesNotNeedBraces() throws Exception {
ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolve(true);
final OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/issue_16223.yaml", null, parseOptions)
.getOpenAPI();
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
codegen.setOpenAPI(openAPI);
Map<String, Schema> schemas = openAPI.getPaths().get("/test").getGet().getParameters().stream()
.collect(Collectors.toMap(
Parameter::getName,
p -> ModelUtils.getReferencedSchema(openAPI, p.getSchema())));
Assert.assertEquals(codegen.toDefaultParameterValue(schemas.get("fileEnumWithDefault")), "A,B");
Assert.assertEquals(codegen.toDefaultParameterValue(schemas.get("fileEnumWithDefaultEmpty")), "");
Assert.assertEquals(codegen.toDefaultParameterValue(schemas.get("inlineEnumWithDefault")), "A,B");
Assert.assertEquals(codegen.toDefaultParameterValue(schemas.get("inlineEnumWithDefaultEmpty")), "");
}
private static Schema<?> createObjectSchemaWithMinItems() {
return new ObjectSchema()
.addProperties("id", new IntegerSchema().format("int32"))

View File

@ -0,0 +1,54 @@
---
openapi: 3.0.3
info:
title: Test
version: 1.0.0-SNAPSHOT
paths:
/test:
get:
parameters:
- name: fileEnumWithDefault
in: query
schema:
$ref: './issue_16223_enum_with_default.yaml'
- name: fileEnumWithDefaultEmpty
in: query
schema:
$ref: './issue_16223_enum_with_default_empty.yaml'
- name: inlineEnumWithDefault
in: query
schema:
type: array
items:
type: string
enum:
- A
- B
- C
default:
- A
- B
- name: inlineEnumWithDefaultEmpty
in: query
schema:
type: array
items:
type: string
enum:
- A
- B
- C
default: []
responses:
"200":
description: OK
components:
schemas:
Test:
type: object
properties:
withDefault:
$ref: './issue_16223_enum_with_default.yaml'
withEmptyDefault:
$ref: './issue_16223_enum_with_default_empty.yaml'

View File

@ -0,0 +1,10 @@
type: array
items:
type: string
enum:
- A
- B
- C
default:
- A
- B

View File

@ -0,0 +1,8 @@
type: array
items:
type: string
enum:
- A
- B
- C
default: []