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

[PJL-10221] handle some edge cases in OpenAPI v3.1 swagger generation #319

Merged
merged 6 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions flask_rebar/swagger_generation/marshmallow_to_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from marshmallow import Schema
from marshmallow.validate import Range
from marshmallow.validate import OneOf
from marshmallow.validate import ContainsOnly
from marshmallow.validate import Length
from marshmallow.validate import Validator

Expand Down Expand Up @@ -423,6 +424,15 @@ class DictConverter(FieldConverter[m.fields.Dict]):
def get_type(self, obj: m.fields.Dict, context: _Context) -> Union[str, List[str]]:
return self.null_type_determination(obj, context, sw.object_)

@sets_swagger_attr(sw.additional_properties)
def get_additional_properties(
self, obj: m.fields.Dict, context: _Context
) -> Union[Type[UNSET], m.fields.Dict]:
if obj.value_field:
return context.convert(obj.value_field, context)
else:
return UNSET


class IntegerConverter(FieldConverter[m.fields.Integer]):
MARSHMALLOW_TYPE = m.fields.Integer
Expand Down Expand Up @@ -604,6 +614,14 @@ def get_maximum(
return UNSET


class ContainsOnlyConverter(ValidatorConverter):
MARSHMALLOW_TYPE = ContainsOnly

@sets_swagger_attr(sw.items)
def get_items(self, obj: ContainsOnly, context: _Context) -> Dict[str, Any]:
return {"type": context.memo["items"]["type"], "enum": obj.choices}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is messy but I couldn't figure out a better way to figure out the type of the parent value when the obj we are passed is the marshmallow.validator i.e. 1 level inside the value.



class OneOfConverter(ValidatorConverter):
MARSHMALLOW_TYPE = OneOf

Expand Down Expand Up @@ -794,6 +812,7 @@ def _common_converters() -> List[MarshmallowConverter]:
"""Instantiates the converters we use in ALL of the registries below"""
converters: List[MarshmallowConverter] = [
BooleanConverter(),
ContainsOnlyConverter(),
DateConverter(),
DateTimeConverter(),
FunctionConverter(),
Expand Down
84 changes: 84 additions & 0 deletions tests/swagger_generation/test_marshmallow_to_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,90 @@ class Foo(m.Schema):
},
)

def test_list_enum_openapi_v3(self):
for field, result in [
(
QueryParamList(m.fields.Integer(), validate=v.ContainsOnly([1, 2, 3])),
{
"type": "array",
"items": {"type": "integer", "enum": [1, 2, 3]},
"explode": True,
},
),
(
CommaSeparatedList(
m.fields.String(), validate=v.ContainsOnly(["a", "b", "c"])
),
{
"type": "array",
"items": {"type": "string", "enum": ["a", "b", "c"]},
"style": "form",
"explode": False,
},
),
]:

class Foo(m.Schema):
a = field

schema = Foo()
json_schema = self.registry.convert(schema, openapi_version=3)

self.assertEqual(
json_schema,
{
"additionalProperties": False,
"type": "object",
"title": "Foo",
"properties": {"a": result},
},
)

def test_custom_dicts_openapi_v3(self):
for field, result in [
(
m.fields.Dict(),
{
"type": "object",
},
),
(
m.fields.Dict(
values=m.fields.Integer(),
),
{
"type": "object",
"additionalProperties": {"type": "integer"},
},
),
(
m.fields.Dict(
keys=m.fields.String(),
values=m.fields.String(),
),
{
"type": "object",
"additionalProperties": {"type": "string"},
},
),
]:

class Foo(m.Schema):
a = field

schema = Foo()
json_schema = self.registry.convert(schema, openapi_version=3)

self.assertEqual(
json_schema,
{
"additionalProperties": False,
"type": "object",
"title": "Foo",
"properties": {"a": result},
},
)

def test_data_key(self):
registry = ConverterRegistry()
registry.register_types(ALL_CONVERTERS)
Expand Down
Loading