Skip to content

Commit

Permalink
fix: setting props on schema using camelCase names (#2800)
Browse files Browse the repository at this point in the history
fix: setting schema props using camelCase names

We have been setting properties on the schema object using camelCase names when they are declared with snake_case names.
  • Loading branch information
peterschutt authored Nov 29, 2023
1 parent 9f6ddf4 commit 77b62a2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
27 changes: 17 additions & 10 deletions litestar/_openapi/schema_generation/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,22 @@
from litestar.plugins import OpenAPISchemaPluginProtocol

KWARG_DEFINITION_ATTRIBUTE_TO_OPENAPI_PROPERTY_MAP: dict[str, str] = {
"content_encoding": "contentEncoding",
"content_encoding": "content_encoding",
"default": "default",
"description": "description",
"enum": "enum",
"examples": "examples",
"external_docs": "externalDocs",
"external_docs": "external_docs",
"format": "format",
"ge": "minimum",
"gt": "exclusiveMinimum",
"gt": "exclusive_minimum",
"le": "maximum",
"lt": "exclusiveMaximum",
"max_items": "maxItems",
"max_length": "maxLength",
"min_items": "minItems",
"min_length": "minLength",
"multiple_of": "multipleOf",
"lt": "exclusive_maximum",
"max_items": "max_items",
"max_length": "max_length",
"min_items": "min_items",
"min_length": "min_length",
"multiple_of": "multiple_of",
"pattern": "pattern",
"title": "title",
}
Expand Down Expand Up @@ -644,7 +644,14 @@ def process_schema_result(self, field: FieldDefinition, schema: Schema) -> Schem
if schema_key == "examples":
value = get_formatted_examples(field, cast("list[Example]", value))

setattr(schema, schema_key, value)
# we only want to transfer values from the `KwargDefinition` to `Schema` if the schema object
# doesn't already have a value for that property. For example, if a field is a constrained date,
# by this point, we have already set the `exclusive_minimum` and/or `exclusive_maximum` fields
# to floating point timestamp values on the schema object. However, the original `date` objects
# that define those constraints on `KwargDefinition` are still `date` objects. We don't want to
# overwrite them here.
if getattr(schema, schema_key, None) is None:
setattr(schema, schema_key, value)

if not schema.examples and self.generate_examples:
from litestar._openapi.schema_generation.examples import create_examples_for_field
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_openapi/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,13 @@ def my_handler(
assert router3.param_in == ParamType.HEADER
assert router3.schema.type == OpenAPIType.NUMBER # type: ignore
assert router3.required
assert router3.schema.multipleOf == 5.0 # type: ignore
assert router3.schema.multiple_of == 5.0 # type: ignore
assert router3.schema.examples # type: ignore

assert controller1.param_in == ParamType.QUERY
assert controller1.schema.type == OpenAPIType.INTEGER # type: ignore
assert controller1.required
assert controller1.schema.exclusiveMaximum == 100.0 # type: ignore
assert controller1.schema.exclusive_maximum == 100.0 # type: ignore
assert controller1.schema.examples # type: ignore

assert controller3.param_in == ParamType.QUERY
Expand Down

0 comments on commit 77b62a2

Please sign in to comment.