Fix default value for enum type in RequestParameter was not added (#10332)

This commit is contained in:
Jorge Rodríguez Martín 2021-09-08 04:16:00 +02:00 committed by GitHub
parent 2640c369e8
commit a24e7e2af3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 146 additions and 1 deletions

View File

@ -1001,7 +1001,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
@Override
public String toDefaultParameterValue(final Schema<?> schema) {
Object defaultValue = schema.getDefault();
Object defaultValue = schema.get$ref() != null ? ModelUtils.getReferencedSchema(openAPI, schema).getDefault() : schema.getDefault();
if (defaultValue == null) {
return null;
}

View File

@ -690,4 +690,37 @@ public class SpringCodegenTest {
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/api/GirafesApi.java"), "allowableValues = \"0, 1\"");
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/api/GirafesApi.java"), "@PathVariable");
}
@Test
public void shouldGenerateDefaultValueForEnumRequestParameter() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/spring/issue_10278.yaml");
final SpringCodegen codegen = new SpringCodegen();
codegen.setOpenAPI(openAPI);
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(SpringCodegen.DELEGATE_PATTERN, "true");
codegen.additionalProperties().put(SpringCodegen.REACTIVE, "true");
ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);
DefaultGenerator generator = new DefaultGenerator();
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
generator.opts(input).generate();
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/api/GetApi.java"),
"@RequestParam(value = \"testParameter1\", required = false, defaultValue = \"BAR\")",
"@RequestParam(value = \"TestParameter2\", required = false, defaultValue = \"BAR\")");
}
}

View File

@ -0,0 +1,112 @@
openapi: 3.0.1
info:
title: Swagger Petstore
description: 'This is a sample server Petstore server. You can find out more about Swagger
at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For
this sample, you can use the api key `special-key` to test the authorization filters.'
termsOfService: http://swagger.io/terms/
contact:
email: apiteam@swagger.io
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.0.0
externalDocs:
description: Find out more about Swagger
url: http://swagger.io
servers:
- url: https://petstore.swagger.io/v2
- url: http://petstore.swagger.io/v2
tags:
- name: pet
description: Everything about your Pets
externalDocs:
description: Find out more
url: http://swagger.io
- name: store
description: Access to Petstore orders
- name: user
description: Operations about user
externalDocs:
description: Find out more about our store
url: http://swagger.io
paths:
/get:
put:
tags:
- pet
summary: Update an existing pet
operationId: getPet
parameters:
- $ref: '#/components/parameters/TestParameter1'
- in: query
name: TestParameter2
schema:
type: string
enum: [ FOO, BAR ]
default: BAR
responses:
400:
description: Invalid ID supplied
content: {}
404:
description: Pet not found
content: {}
405:
description: Validation exception
content: {}
security:
- petstore_auth:
- write:pets
- read:pets
x-codegen-request-body-name: body
components:
parameters:
TestParameter1:
name: testParameter1
in: query
description: |
Type of token
schema:
$ref: '#/components/schemas/TestParameter1'
schemas:
TestParameter1:
type: string
enum:
- FOO
- BAR
default: BAR
Category:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
xml:
name: Category
Tag:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
xml:
name: Tag
securitySchemes:
petstore_auth:
type: oauth2
flows:
implicit:
authorizationUrl: http://petstore.swagger.io/oauth/dialog
scopes:
write:pets: modify pets in your account
read:pets: read your pets
api_key:
type: apiKey
name: api_key
in: header