[BUG] openapi-yaml translates "NO" to false
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
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"
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:
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 }
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
Alright, thank you for the detailed answer! I will close the issue.