Skip to content

Commit b660037

Browse files
authored
Merge branch 'main' into gh-8-disciminator
2 parents f67ea52 + 25f4d9c commit b660037

5 files changed

Lines changed: 89 additions & 7 deletions

File tree

src/openapi_parser/builders/schema.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def __init__(self, strict_enum: bool = True) -> None:
154154
def create(self, data: dict[str, Any]) -> Schema:
155155
"""Create a schema object from a raw dict."""
156156
data = merge_all_of_schemas(data)
157+
not_data = data.pop("not", None)
157158

158159
if "oneOf" in data:
159160
data["type"] = DataType.ONE_OF
@@ -181,7 +182,12 @@ def create(self, data: dict[str, Any]) -> Schema:
181182

182183
logger.debug(f"Building schema [type={data_type}]")
183184

184-
return builder_func(data)
185+
schema = builder_func(data)
186+
187+
if not_data is not None:
188+
schema.not_schema = self.create(not_data)
189+
190+
return schema
185191

186192
def _null(self, data: dict[str, Any]) -> Null:
187193
return Null(**extract_attrs(data, {}))

src/openapi_parser/specification.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,9 @@ class Schema:
9494
read_only: bool | None = field(default=False)
9595
write_only: bool | None = field(default=False)
9696
deprecated: bool | None = field(default=False)
97+
not_schema: Schema | None = None
9798
extensions: dict[str, Any] | None = field(default_factory=dict)
9899

99-
# all_of: Any # TODO
100-
# one_of: Any # TODO
101-
# any_of: Any # TODO
102-
# not: Any # TODO
103-
104100

105101
@dataclass
106102
class Integer(Schema):

tests/builders/test_not.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from typing import Any
2+
3+
import pytest
4+
5+
from openapi_parser.builders.schema import SchemaFactory
6+
from openapi_parser.enumeration import DataType
7+
from openapi_parser.specification import (
8+
AnyOf,
9+
Array,
10+
Boolean,
11+
Integer,
12+
Number,
13+
Object,
14+
Property,
15+
Schema,
16+
String,
17+
)
18+
19+
data_provider = (
20+
(
21+
{
22+
"type": "string",
23+
"not": {"type": "integer"},
24+
},
25+
String(
26+
type=DataType.STRING,
27+
not_schema=Integer(type=DataType.INTEGER),
28+
),
29+
),
30+
(
31+
{
32+
"type": "object",
33+
"properties": {
34+
"name": {"type": "string"},
35+
},
36+
"not": {"type": "integer"},
37+
},
38+
Object(
39+
type=DataType.OBJECT,
40+
properties=[
41+
Property(name="name", schema=String(type=DataType.STRING)),
42+
],
43+
not_schema=Integer(type=DataType.INTEGER),
44+
),
45+
),
46+
(
47+
{
48+
"not": {"type": "integer"},
49+
},
50+
AnyOf(
51+
type=DataType.ANY_OF,
52+
schemas=[
53+
Integer(type=DataType.INTEGER),
54+
Number(type=DataType.NUMBER),
55+
String(type=DataType.STRING),
56+
Boolean(type=DataType.BOOLEAN),
57+
Array(type=DataType.ARRAY),
58+
Object(type=DataType.OBJECT),
59+
],
60+
not_schema=Integer(type=DataType.INTEGER),
61+
),
62+
),
63+
(
64+
{
65+
"type": "string",
66+
},
67+
String(type=DataType.STRING),
68+
),
69+
)
70+
71+
72+
@pytest.mark.parametrize(["data", "expected"], data_provider)
73+
def test_not_builder(data: dict[str, Any], expected: Schema) -> None:
74+
factory = SchemaFactory()
75+
assert expected == factory.create(data)

tests/data/swagger.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ components:
120120
allowReserved: true
121121
schema:
122122
type: integer
123+
not:
124+
type: string
123125

124126
Offset:
125127
name: offset

tests/openapi_fixture.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,10 @@ def create_specification() -> Specification:
354354
style=QueryParameterStyle.FORM,
355355
allow_reserved=True,
356356
example=10,
357-
schema=Integer(type=DataType.INTEGER),
357+
schema=Integer(
358+
type=DataType.INTEGER,
359+
not_schema=String(type=DataType.STRING),
360+
),
358361
),
359362
Parameter(
360363
name="offset",

0 commit comments

Comments
 (0)