swagger-api / swagger-api/swagger-codegen

[JAVA] Broken code is generated if exclude (using "|") description of requestBody in swagger code generation

Open
#11,748 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

In case requestBody has very descriptive text (in order to display detailed information on swagger GUI) then we may want to exclude such big content from code generation.

When I am trying to exclude such description from requestBody then broken code(not able to compile) is generated.

Swagger-codegen version

3.0.33

Swagger declaration file content or url

If you post the code inline, please wrap it with

/orders/search:
    post:
      summary: Search for Orders.
      operationId: search_orders
      tags:
        - SearchOrder
      parameters:
        - $ref: '#/components/parameters/limit'
        - $ref: '#/components/parameters/cursor'
      requestBody:
        $ref: "#/components/requestBodies/SearchObjectBody"
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                title: SearchOrderResponse
                properties:
                  items:
                    type: array
                    items:
                      $ref: '#/components/schemas/order'
                  response_metadata:
                    $ref: "#/components/schemas/PaginatedResponseMetadata"
requestBodies:
    SearchObjectBody:
      description: |
        Long description for describing Filter expression for 
        searching object <Describe expression rules>
      content:
        application/json:
          schema:
            x-body-name: filter_exression
            properties:
              filter_expression:
                type: string
                minLength: 5
                maxLength: 2000
                example: string_field CONTAINS "over" AND numberic_field GT 9000 OR string_field2 EQ "Goku"
Command line used for generation

../gradlew generateSwaggerCode
swaggerCodegen group: 'io.swagger.codegen.v3', name: 'swagger-codegen-cli', version: '3.0.33'

Steps to reproduce

Step1:
In swagger declaration yaml, add request body description as follows:
requestBodies:
SearchObjectBody:
description: |
Filter expression for searching object

Step2:
Generate Java code using SwaggerCodegen
Step3:
Check the generated java file SearchOrderAPI
Observations:

  • Compilation errors
  • Description is added in generated code even-though in swagger declaration is is marked to skip(|)
  • Description string added in generated java file is not closed with quotes ("") properly.
Suggest a fix/enhancement

Similar to examples, it should be allowed to skip description in java code generation if user defines description like:
requestBodies:
SearchObjectBody:
description: |
Filter expression for searching object

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the supplied YAML using ../gradlew generateSwaggerCode and inspect the generated SearchOrderAPI file. Trace the generator or template entry point that handles the requestBody description, then verify that the generated Java compiles and does not include the multiline description when the YAML uses the reported form.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.