Skip to content

Add support for booleans and floats in OpenAPI Schema const #1086

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

Merged
merged 6 commits into from
Aug 25, 2024

Conversation

flxdot
Copy link
Contributor

@flxdot flxdot commented Jul 31, 2024

We encountered the problem that the Pydantic Schemas for the OpenAPI spec, do not support the const keyword if it is of type boolean.

Why do I think it should be supported?

The OpenAPI Specification explicitly marks the const keyword as unsupported. Nevertheless it should not result in exceptions if the keyword is present.

I furthermore did not find any evidence in JSON Schema that would limit the types valid for the const keyword to be string or integer only.

Thus I suggest allowing boolean as well as floats as constant values.

Example

The following is minimal example in FastAPI that would produce such a schema:

from typing import Literal

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel, Field


class ResponseWithConstantBool(BaseModel):

    message: str = Field(...)
    is_welcome: Literal[True] = Field(True)


app = FastAPI()


@app.get("/", response_model=ResponseWithConstantBool)
async def root():
    return {"message": "Hello World"}


if __name__ == "__main__":
    uvicorn.run(app)

Generated OpenAPI Specification

{
  "openapi": "3.1.0",
  "info": {
    "title": "FastAPI",
    "version": "0.1.0"
  },
  "paths": {
    "/": {
      "get": {
        "summary": "Root",
        "operationId": "root__get",
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ResponseWithConstantBool"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ResponseWithConstantBool": {
        "properties": {
          "message": {
            "type": "string",
            "title": "Message"
          },
          "is_welcome": {
            "type": "boolean",
            "enum": [true],
            "const": true,
            "title": "Is Welcome",
            "default": true
          }
        },
        "type": "object",
        "required": [
          "message"
        ],
        "title": "ResponseWithConstantBool"
      }
    }
  }
}

@flxdot flxdot changed the title feat: Add support for booleans and floats in OpenAPI Schema const fix: Add support for booleans and floats in OpenAPI Schema const Aug 1, 2024
@dbanty dbanty changed the title fix: Add support for booleans and floats in OpenAPI Schema const Add support for booleans and floats in OpenAPI Schema const Aug 25, 2024
Copy link
Collaborator

@dbanty dbanty left a comment

Choose a reason for hiding this comment

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

I believe OpenAPI 3.1 is what added support for const, but you're right, there's no reason it shouldn't support all the primitives. Thanks for the PR!

@dbanty dbanty enabled auto-merge August 25, 2024 06:10
@dbanty dbanty added this pull request to the merge queue Aug 25, 2024
Merged via the queue into openapi-generators:main with commit e4128ac Aug 25, 2024
19 checks passed
@knope-bot knope-bot bot mentioned this pull request Aug 25, 2024
github-merge-queue bot pushed a commit that referenced this pull request Aug 25, 2024
> [!IMPORTANT]
> Merging this pull request will create this release

## Fixes

### Allow OpenAPI 3.1-style `exclusiveMinimum` and `exclusiveMaximum`

Fixed by PR #1092. Thanks @mikkelam!

### Add missing `cast` import when using `const`

Fixed by PR #1072. Thanks @dorcohe!

### Correctly resolve references to a type that is itself just a single
allOf reference

PR #1103 fixed issue #1091. Thanks @eli-bl!

### Support `const` booleans and floats

Fixed in PR #1086. Thanks @flxdot!

Co-authored-by: knope-bot[bot] <152252888+knope-bot[bot]@users.noreply.github.com>
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.

3 participants