OpenAPITools / OpenAPITools/openapi-generator

[BUG] OpenAPI.yaml generated returns strings without double quotes and "no" string is implicitly interpreted as boolean

Open
#3,196 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

openapi.yaml file generated in the project with {{{openapi-yaml}}} like https://github.com/OpenAPITools/openapi-generator/blob/20b8eff6e3dcec5dfe955b5dda1e4b84d8bdce44/modules/openapi-generator/src/main/resources/python-flask/openapi.mustache doesn't use double quotes around string values.
Because in YAML specification, it seems value true/false, yes/no and on/off can be implicitly convert to boolean unless the value have double quotes.
Here the code in PyYAML: https://github.com/yaml/pyyaml/blob/4c2e993321ad29a02a61c4818f3cef9229219003/lib3/yaml/resolver.py#L170-L175

Example: The yaml generated should return - "no" instead of - no to avoid implicit convertion.

openapi-generator version

4.0.3-beta
It can be a regression because it was not present in swagger-codegen

OpenAPI declaration file content or url & Command line used for generation

I am using the online version to generate python-flask server with the following JSON openapi.quote.json.zip

{
  "host": "HOST",
  "info": {
    "description": "Car example",
    "title": "Car API",
    "version": "1.0.0"
  },
  "definitions": {
    "car": {
      "properties": {
        "name": {
          "description": "The name of the car.",
          "maxLength": 255,
          "type": "string"
        },
        "test": {
          "description": "Buggy enum: no becomes False",
          "enum": [
            "no",
            "great",
            "other"
          ],
          "type": "string"
        }
      },
      "required": [
        "name",
        "test"
      ]
    }
  },
  "paths": {
    "/v1/cars": {
      "post": {
        "parameters": [
          {
            "in": "body",
            "name": "body",
            "required": true,
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/car"
              }
            }
          }
        ],
        "consumes": [
          "application/json"
        ],
        "responses": {
          "200": {
            "description": "Successfully updated"
          }
        },
        "summary": "Add cars",
        "tags": [
          "Cars"
        ]
      }
    }
  },
  "schemes": [
    "https"
  ],
  "swagger": "2.0"
}
Steps to reproduce
curl -H "Content-type: application/json" -X POST --data-binary @openapi.quote.json http://api-latest-master.openapi-generator.tech/api/gen/servers/python-flask
curl http://api-latest-master.openapi-generator.tech/api/gen/download/298f2e1b-447c-41c1-841f-6544899e3043 -o server.zip
unzip server.zip
cd python-flask-server
cat car_api/openapi/openapi.yaml
python3 -m venv venv
. ./venv/bin/activate
pip install -r requirements.txt
# Run the server
python3 -m car_api

# In other terminal
# "great" value should work and it works: returns "do some magic!"
curl http://localhost:8080/v1/cars -XPOST -H 'Content-type: application/json' -d '[{"name": "car name", "test": "great"}]'

# "fail" value should fail and it fails as expected
curl http://localhost:8080/v1/cars -XPOST -H 'Content-type: application/json' -d '[{"name": "car name", "test": "fail"}]'

# "no" value should work and it fails: returns "detail": "'no' is not one of [False, 'great', 'other']",
curl http://localhost:8080/v1/cars -XPOST -H 'Content-type: application/json' -d '[{"name": "car name", "test": "no"}]'

# Add doubles quotes:
sed -i 's/- \(yes\|Yes\|YES\|no\|No\|NO\|true\|True\|TRUE\|false\|False\|FALSE\|on\|On\|ON\|off\|Off\|OFF\)$/- "\1"/g' car_api/openapi/openapi.yaml

# Run the server with the update applied
python3 -m car_api

# In other terminal
# "no" value should work and it works: "do some magic!"
curl http://localhost:8080/v1/cars -XPOST -H 'Content-type: application/json' -d '[{"name": "car name", "test": "no"}]'

Generated file by openapi generator:

openapi: 3.0.1
info:
  description: Car example
  title: Car API
  version: 1.0.0
servers:
- url: https://HOST/
paths:
  /v1/cars:
    post:
      operationId: v1_cars_post
      requestBody:
        content:
          application/json:
            schema:
              items:
                $ref: '#/components/schemas/car'
              type: array
        required: true
      responses:
        200:
          content: {}
          description: Successfully updated
      summary: Add cars
      tags:
      - Cars
      x-codegen-request-body-name: body
      x-openapi-router-controller: car_api.controllers.cars_controller
components:
  schemas:
    car:
      properties:
        name:
          description: The name of the car.
          maxLength: 255
          type: string
        test:
          description: 'Buggy enum: no becomes False'
          enum:
          - no
          - great
          - other
          type: string
      required:
      - name
      - test
      type: object
Related issues/PRs

Maybe the same error in https://github.com/OpenAPITools/openapi-generator/issues/2870

Suggest a fix

Add double quotes ?

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

Start with modules/openapi-generator/src/main/resources/python-flask/openapi.mustache and reproduce the issue using the linked openapi.quote.json example and the documented python-flask generation commands. Inspect the generated car_api/openapi/openapi.yaml, then verify that the enum value "no" remains a string and the generated server accepts it without the manual quote substitution.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, java, openapi, python, yaml
Domain
api, backend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.