Skip to content

Commit

Permalink
[Java] Fix default values of array-type parameters in a referenced fi…
Browse files Browse the repository at this point in the history
…le (#17779)

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

* add test
  • Loading branch information
kota65535 committed Feb 7, 2024
1 parent d0ed25a commit 795f079
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,9 @@ public String toDefaultParameterValue(final Schema<?> schema) {
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)
Expand All @@ -1297,6 +1300,11 @@ public String toDefaultParameterValue(final Schema<?> schema) {
.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -873,6 +877,26 @@ public void testOneOfModelImports() throws Exception {
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"))
Expand Down
54 changes: 54 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue_16223.yaml
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type: array
items:
type: string
enum:
- A
- B
- C
default:
- A
- B
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type: array
items:
type: string
enum:
- A
- B
- C
default: []

0 comments on commit 795f079

Please sign in to comment.