swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.03k stars 6.03k forks source link

[Spring] allowableValues uppercase #4550

Open samogot opened 7 years ago

samogot commented 7 years ago
Description

I'm getting uppercased enum values instead of case used in spec. For example:

   ResponseEntity<Text> chapterTextGet(@ApiParam(value = "Pipe delimited list of needed fields.", allowableValues = "TEXTID, TEXTWIKI, TEXTHTML, CONTENTS, FOOTNOTES") @RequestParam(value = "fields", required = false) List<String> fields);

Expected:

   ResponseEntity<Text> chapterTextGet(@ApiParam(value = "Pipe delimited list of needed fields.", allowableValues = "textId, textWiki, textHtml, contents, footnotes") @RequestParam(value = "fields", required = false) List<String> fields);

This values are names of class fields, so case-sensitiveness is important for me

Swagger-codegen version

current master

Swagger declaration file content or url
swagger: '2.0'
info:
  version: '1.0.0'
  title: Test API
host: localhost:8080
basePath: /api
schemes:
  - http
consumes:
  - application/json
produces:
  - application/json
paths:
  /chapterText:
    get:
      parameters:
        - name: fields
          in: query
          description: Pipe delimited list of needed fields.
          required: false
          type: array
          items:
            type: string
            enum:
              - textId
              - textWiki
              - textHtml
              - contents
              - footnotes
          collectionFormat: pipes
          default:
            - textHtml
            - contents
            - footnotes
      responses:
        '200':
          description: text response
          schema:
            $ref: '#/definitions/Text'
definitions:
  Text:
    type: object
    properties:
      textId:
        type: integer
      textWiki:
        type: string
      textHtml:
        type: string
      contents:
        type: string
      footnotes:
        type: string
Command line used for generation
java -jar ~/Downloads/swagger-codegen-cli-2.2.1.jar generate -l spring -i test.yaml -o test/
Steps to reproduce
  1. Generate spring-boot server stub using command line script and spec file above
  2. Look at src/main/java/io/swagger/api/ChapterTextApi.java
Related issues

Don't think it really related, but it may by somewhere near https://github.com/swagger-api/swagger-codegen/issues/3792

wing328 commented 7 years ago

Here are the related files that you need to fix:

modules/swagger-codegen/src/main/resources/JavaSpring/formParams.mustache:

{{#isFormParam}}{{#notFile}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}} {{#allowableValues}}, allowableValues="{{#enumVars}}{{{name}}}{{^-last}}, {{/-last}}{{#-last}}{{/-last}}{{/enumVars}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @RequestPart(value="{{paramName}}"{{#required}}, required=true{{/required}}{{^required}}, required=false{{/required}})  {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}@ApiParam(value = "file detail") @RequestPart("file") MultipartFile {{baseName}}{{/isFile}}{{/isFormParam}}

modules/swagger-codegen/src/main/resources/JavaSpring/headerParams.mustache:

{{#isHeaderParam}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}} {{#allowableValues}}, allowableValues="{{#enumVars}}{{{name}}}{{^-last}}, {{/-last}}{{#-last}}{{/-last}}{{/enumVars}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @RequestHeader(value="{{baseName}}", required={{#required}}true{{/required}}{{^required}}false{{/required}}) {{{dataType}}} {{paramName}}{{/isHeaderParam}}
modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-cloud/formParams.mustache:{{#isFormParam}}{{#notFile}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}} {{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @RequestParam(value="{{paramName}}"{{#required}}, required=true{{/required}}{{^required}}, required=false{{/required}})  {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}@ApiParam(value = "file detail") @RequestParam("file") MultipartFile {{baseName}}{{/isFile}}{{/isFormParam}}

modules/swagger-codegen/src/main/resources/JavaSpring/pathParams.mustache:

{{#isPathParam}}{{#useBeanValidation}}{{>beanValidationPathParams}}{{/useBeanValidation}}@ApiParam(value = "{{{description}}}"{{#required}},required=true{{/required}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}} {{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @PathVariable("{{paramName}}") {{{dataType}}} {{paramName}}{{/isPathParam}}
modules/swagger-codegen/src/main/resources/JavaSpring/queryParams.mustache:{{#isQueryParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}@ApiParam(value = "{{{description}}}"{{#required}}, required = true{{/required}}{{#allowableValues}}, allowableValues = "{{#enumVars}}{{{name}}}{{^-last}}, {{/-last}}{{#-last}}{{/-last}}{{/enumVars}}"{{/allowableValues}}{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) @RequestParam(value = "{{paramName}}"{{#required}}, required = true{{/required}}{{^required}}, required = false{{/required}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) {{{dataType}}} {{paramName}}{{/isQueryParam}}

modules/swagger-codegen/src/main/resources/JavaSpring/bodyParams.mustache:

{{#isBodyParam}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}} {{^isContainer}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{/isContainer}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @RequestBody {{{dataType}}} {{paramName}}{{/isBodyParam}}

modules/swagger-codegen/src/main/resources/JavaSpring/queryParams.mustache:

{{#isQueryParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}@ApiParam(value = "{{{description}}}"{{#required}}, required = true{{/required}}{{#allowableValues}}, allowableValues = "{{#enumVars}}{{{name}}}{{^-last}}, {{/-last}}{{#-last}}{{/-last}}{{/enumVars}}"{{/allowableValues}}{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) @RequestParam(value = "{{paramName}}"{{#required}}, required = true{{/required}}{{^required}}, required = false{{/required}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) {{{dataType}}} {{paramName}}{{/isQueryParam}}

You will need to change the following

"{{#enumVars}}{{{name}}}{{^-last}}, {{/-last}}{{#-last}}{{/-last}}{{/enumVars}}"

to

"{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{#-last}}{{/-last}}{{/enumVars}}"

Then it should fix it as {{{value}}} should hold the original enum value.

Would you have time to contribute the suggested fix?