Skip to content

Commit

Permalink
Implement primitive unions (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche committed Jun 6, 2023
1 parent d8161a0 commit 871e7be
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/_static/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ div.tab-set pre {
padding: 1.25em;
}

body:not([data-theme="light"]) .highlight {
body .highlight.only_dark {
background: #18181a;
}
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The **third number** is for emergencies when we need to start branches for older
- Add OpenAPI support for generic _attrs_ classes.
- Add OpenAPI support for unions of a single _attrs_ class and `None` (optionals).
- Properly set the OpenAPI `required` attribute for _attrs_ fields without defaults.
- Add OpenAPI support for primitive types in unions.

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions src/uapi/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,8 @@ def _build_attrs_schema(
refs.append(Reference(ref))
elif arg is NoneType:
refs.append(InlineType(Schema.Type.NULL))
elif arg in PYTHON_PRIMITIVES_TO_OPENAPI:
refs.append(InlineType(PYTHON_PRIMITIVES_TO_OPENAPI[arg].type))
schema = OneOfSchema(refs)
else:
continue
Expand Down
2 changes: 2 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class SumTypesRequestInner:
a: int

inner: SumTypesRequestInner | None
opt_string: str | None
opt_def_string: str | None = None


@define
Expand Down
10 changes: 8 additions & 2 deletions tests/test_openapi_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,15 @@ def test_sum_types_model(app_factory) -> None:
Reference("#/components/schemas/SumTypesRequestInner"),
InlineType(Schema.Type.NULL),
]
)
),
"opt_string": OneOfSchema(
[InlineType(Schema.Type.STRING), InlineType(Schema.Type.NULL)]
),
"opt_def_string": OneOfSchema(
[InlineType(Schema.Type.STRING), InlineType(Schema.Type.NULL)]
),
},
required=["inner"],
required=["inner", "opt_string"],
)
assert spec.components.schemas["SumTypesRequestInner"] == Schema(
Schema.Type.OBJECT,
Expand Down

0 comments on commit 871e7be

Please sign in to comment.