Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: OpenAPI YAML converts strings to boolean #5377

Open
4 tasks done
kolypto opened this issue Dec 16, 2024 · 3 comments
Open
4 tasks done

[Bug]: OpenAPI YAML converts strings to boolean #5377

kolypto opened this issue Dec 16, 2024 · 3 comments
Labels
bug Something isn't working emitter:openapi3 Issues for @typespec/openapi3 emitter triaged:core

Comments

@kolypto
Copy link

kolypto commented Dec 16, 2024

Describe the bug

TypeSpec converts "no" enum strings to "False", which is incorrect.

This is the so-called Norway Problem:

countries:
- GB
- NO  // Becomes false

The safest way would be to use "string" quotes for enum string values that convert to a boolean, case-insensitive:

y|yes|n|no|true|false|on|off

Reproduction

This TypeSpec file:

@service({})
namespace Main;

enum Countries {
    GB: "gb",
    NO: "no",   // !!! Norway problem
}

gets compiled into this OpenAPI:

components:
  schemas:
    Countries:
      type: string
      enum:
        - gb
        - no   # !!! Becomes boolean "false", but must be a string

Playground: link

Or compile locally this way:

$ npm install @typespec/compiler @typespec/openapi3
$ tsp compile --emit @typespec/openapi3 .

Checklist

@kolypto kolypto added the bug Something isn't working label Dec 16, 2024
@markcowl markcowl added the emitter:openapi3 Issues for @typespec/openapi3 emitter label Dec 16, 2024
@markcowl markcowl added this to the [2025] January milestone Dec 16, 2024
@wanlwanl
Copy link
Member

@kolypto I try it in Playground, but it looks like want you want. Could you double confirm?

@kolypto
Copy link
Author

kolypto commented Dec 19, 2024

@wanlwanl it looks alright, but it won't work alright :)

Image

Indeed, the Yaml list is rendered this way:

      enum:
        - gb
        - no

However, in Yaml, this will result in this JSON: [ "gb", false ] because no in Yaml is a boolean value! :)
It should be quoted in the output:

      enum:
        - gb
        - "no"

@wanlwanl
Copy link
Member

@kolypto Thank you for your explanation. This is indeed related to the YAML standard specifications. YAML 1.1 treats yes/no as boolean values, while YAML 1.2 does not. The issue in typespec should indeed be addressed to support YAML 1.1 properly. For now, a temporary workaround could be explicitly specifying YAML version 1.2.x doc section 1.6.8.
FYI:
https://yaml.org/spec/1.1/current.html#id864510
https://yaml.org/spec/1.2.2/#3232-scalar-formats
https://perlpunk.github.io/yaml-test-schema/schemas.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working emitter:openapi3 Issues for @typespec/openapi3 emitter triaged:core
Projects
None yet
Development

No branches or pull requests

3 participants