-
-
Notifications
You must be signed in to change notification settings - Fork 228
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
dbanty
merged 6 commits into
openapi-generators:main
from
flxdot:feat/support-bool-on-const
Aug 25, 2024
Merged
Add support for booleans and floats in OpenAPI Schema const
#1086
dbanty
merged 6 commits into
openapi-generators:main
from
flxdot:feat/support-bool-on-const
Aug 25, 2024
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dozer74
approved these changes
Jul 31, 2024
const
const
const
const
dbanty
approved these changes
Aug 25, 2024
There was a problem hiding this 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!
Merged
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>
micha91
pushed a commit
to micha91/openapi-python-client
that referenced
this pull request
May 13, 2025
…i-generators#1086) 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](https://swagger.io/docs/specification/data-models/keywords/) 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](https://json-schema.org/understanding-json-schema/reference/const) 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: ```python 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 ```json { "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" } } } } ``` --------- Co-authored-by: Felix Fanghaenel <[email protected]> Co-authored-by: Dylan Anthony <[email protected]> Co-authored-by: Dylan Anthony <[email protected]>
micha91
pushed a commit
to micha91/openapi-python-client
that referenced
this pull request
May 13, 2025
> [!IMPORTANT] > Merging this pull request will create this release ## Fixes ### Allow OpenAPI 3.1-style `exclusiveMinimum` and `exclusiveMaximum` Fixed by PR openapi-generators#1092. Thanks @mikkelam! ### Add missing `cast` import when using `const` Fixed by PR openapi-generators#1072. Thanks @dorcohe! ### Correctly resolve references to a type that is itself just a single allOf reference PR openapi-generators#1103 fixed issue openapi-generators#1091. Thanks @eli-bl! ### Support `const` booleans and floats Fixed in PR openapi-generators#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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We encountered the problem that the Pydantic Schemas for the OpenAPI spec, do not support the
const
keyword if it is of typeboolean
.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 bestring
orinteger
only.Thus I suggest allowing
boolean
as well asfloats
as constant values.Example
The following is minimal example in FastAPI that would produce such a schema:
Generated OpenAPI Specification