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

Strings "yes" and "no" serialized as boolean #3372

Open
jimirocks opened this issue Dec 3, 2019 · 4 comments
Open

Strings "yes" and "no" serialized as boolean #3372

jimirocks opened this issue Dec 3, 2019 · 4 comments

Comments

@jimirocks
Copy link

jimirocks commented Dec 3, 2019

The strings "yes" and "no" are serialized wrongly to YAML, when for instance used in enums. Seems the underlying jackson library works well until configured to serve OpenAPI purposes.

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import io.swagger.v3.core.util.Yaml

fun main(args: Array<String>) {
    val yamlWriter = Yaml.pretty()
    println(yamlWriter.writeValueAsString("no"))

    println(ObjectMapper(YAMLFactory()).writeValueAsString("no"))
}

Produces following output:

no

--- "no"

The first one is unfortunately YAML's boolean...

@webron
Copy link
Contributor

webron commented Dec 3, 2019

That's inaccurate. In YAML 1.1 has an extension that allows interpreting on, off, yes, no (with capitalization variations) as boolean values, but it's not part of the core spec. In YAML 1.2 it doesn't exist at all. While for clarity we should quote those values, it can also be overcome by using the right YAML parser (with proper configuration).

@jimirocks
Copy link
Author

jimirocks commented Jan 28, 2020

Well, you are right. But I would assume that swagger library would parse what it serializes into the same semantic. But that's apparently not possible. Try this snippet

import io.swagger.v3.core.util.Yaml
import io.swagger.v3.oas.models.Components
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.media.StringSchema
import io.swagger.v3.parser.OpenAPIV3Parser

fun main(args: Array<String>) {
    val yamlWriter = Yaml.pretty()

    val schema = StringSchema().addEnumItem("yes").addEnumItem("no")
    val openApi = OpenAPI().apply {
        components = Components().addSchemas("testIt", schema)
    }
    val stringOpenApi = yamlWriter.writeValueAsString(openApi)
    println(stringOpenApi)

    val parseResult = OpenAPIV3Parser().readContents(stringOpenApi)
    println(parseResult.openAPI.components.schemas.get("testIt")?.enum)
}

Producing

openapi: 3.0.1
components:
  schemas:
    testIt:
      type: string
      enum:
      - yes
      - no

[true, false]

This doesn't feel correct to me - I created string schema with enums values "yes" and "no" but after serialization roudntrip using the same library my "yes" and "no" turned to "true" and "false".

So please either add clarifying quotes, or make the parser compatible with the serializer (this is IMHO impossible). The option would be to use YAML 1.2 - which would be nice, but it seems the underlying jackson doesn't support it yet.

@jimirocks
Copy link
Author

The current workaround is

val yamlWriter = Yaml.pretty().apply {
        (factory as YAMLFactory).configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, false)
    }

This uses all the configuration from swagger library but the shaved quoting.

@gracekarina
Copy link
Contributor

related swagger-api/swagger-parser#1205

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants