Skip to content

Conversation

@BITespresso
Copy link
Contributor

Fixes issue #241 so that for OpenAPI 3.0.3 the const keyword gets replaced be a single valued enum.

openapi = "3.0.1",
components = Some(components)
).asJson
val Right(json) = readJson("/spec/3.0/const_and_enum.json"): @unchecked
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this correctly rendered, since in components we define both a const and an enum, and the const_and_enum.json file contains only one enum?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is that in order to get rid of the const we need to replace the enum. As the Schema itself does not forbid to have a const and an enum defined for the same item (which of course makes not sense) we will just overwrite the enum with the value defined in const. Hence, what were two different things in the Schema (const and enum) will be reduced to just an enum in the JSON.

Does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or explained by example:

For this schemaComponent

schemaComponent("const and enum")(
  Schema(
    const = Some("const1").map(ExampleSingleValue(_)),
    `enum` = Some(List("enum1", "enum2").map(ExampleSingleValue(_)))
  )
)

the following would be the JSON for OpenAPI 3.1.0 (although it does not make sense to have something being an enum and const at the same time):

{
  "openapi" : "3.1.0",
  "info" : {
    "title" : "API",
    "version" : "1.0.0"
  },
  "components" : {
    "schemas" : {
      "const and enum" : {
        "description" : "const and enum",
        "enum" : [
          "enum1",
          "enum2"
        ],
        "const" : "const1"
      }
    }
  }
}

for OpenAPI 3.0.x this will be transformed to:

{
  "openapi" : "3.0.1",
  "info" : {
    "title" : "API",
    "version" : "1.0.0"
  },
  "components" : {
    "schemas" : {
      "const and enum" : {
        "description" : "const and enum",
        "enum" : [
          "const1"
        ]
      }
    }
  }
}

The above is only an edge case test. In reality it is more important to have the correct result for the "full 3.0 schema" and "full 3.1 schema" tests.

in "/spec/3.1/schema.json"

      "const" : {
        "const" : "const1",
        "description" : "const"
      },

versus in "/spec/3.0/schema.json"

      "const" : {
        "description" : "const",
        "enum" : [
          "const1"
        ]
      }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok the edge case mislead me :)

@adamw adamw merged commit e45c720 into softwaremill:master Jun 27, 2025
3 checks passed
@adamw
Copy link
Member

adamw commented Jun 27, 2025

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants