openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] openapi-yaml translates "NO" to false

Open detached opened this issue 1 year ago • 3 comments

Description

If a enum contains the value "NO" the output yaml will contain "false".

openapi-generator version

v7.5.0

OpenAPI declaration file content or url
openapi: 3.0.2
info:
  title: sample
  version: 1.0.0
servers:
  - url: https://localhost
    description: test
paths:
  /items:
     get:
       responses:
         200:
           description: ""
       parameters:
         - in: query
           name: sort
           description: Sort order
           schema:
             type: string
             enum: 
               - NO
Generation Details
java -jar openapi-generator-cli.jar generate -i ./api.yaml -g openapi-yaml -o ./

Output:

[...]
schema:
  enum:
    - "false"
  type: string
[...]
Steps to reproduce
  • Define a enum with "NO" as value in your spec
  • Generate yaml with "openapi-yaml" generator
  • Output contains enum value "false"
Related issues/PRs
Suggest a fix

detached avatar Apr 26 '24 12:04 detached

That might be a YAML thing:

YAML indicates boolean values with the keywords True, On and Yes for true. False is indicated with False, Off, or No.

https://www.cloudbees.com/blog/yaml-tutorial-everything-you-need-get-started#booleans

Try with quotes?

schema:
   type: string
   enum: 
      - "NO"

stefan521 avatar May 10 '24 21:05 stefan521

You are right, we fixed it with quotes. But I think it is a little surprising when you define a enum for locales like

- DE
- EN
- NO
- FR

and openapi-generator changes Norway to false. :smile:

detached avatar May 15 '24 08:05 detached

Happy to hear you fixed it!

This is not a bug in the open api generator. The problem is how YAML files work. The generator uses a YAML parser. Since YAML 1.1 the words no and off are treated as false. I agree 100% that it is confusing and it should not do this, but it is the intended behaviour.

You can see this issue of NOrway's country code becoming false in YAML here https://www.codethink.co.uk/articles/2021/yaml-schemas/

The two ways to fix it are: Using quotes Forcing the string type like !!str NO

There is also the option to write the spec in JSON rather than YAML. In a way JSON is better because it doesn’t do unpredictable things but it clutters your spec with { braces }

stefan521 avatar May 15 '24 21:05 stefan521

I did a bit of googling and this is is called the "YAML Norway problem". In YAML 1.2 this does not happen and only true and false are treated as boolean values. You could try to explicitly set the version of your document to 1.2

stefan521 avatar May 16 '24 06:05 stefan521

Alright, thank you for the detailed answer! I will close the issue.

detached avatar May 16 '24 06:05 detached