Skip to content

Commit 65a6f2c

Browse files
authored
Merge pull request #2 from mike-oakley/parameter-location-enum
Parameter location enum
2 parents 836e406 + 8c8c364 commit 65a6f2c

File tree

7 files changed

+40
-10
lines changed

7 files changed

+40
-10
lines changed

openapi_schema_pydantic/v3/v3_0_3/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from .path_item import PathItem
1818
from .operation import Operation
1919
from .external_documentation import ExternalDocumentation
20-
from .parameter import Parameter
20+
from .parameter import Parameter, ParameterLocation
2121
from .request_body import RequestBody
2222
from .media_type import MediaType
2323
from .encoding import Encoding

openapi_schema_pydantic/v3/v3_0_3/header.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pydantic import Extra, Field
22

3-
from .parameter import Parameter
3+
from .parameter import Parameter, ParameterLocation
44

55

66
class Header(Parameter):
@@ -14,7 +14,7 @@ class Header(Parameter):
1414
"""
1515

1616
name = Field(default="", const=True)
17-
param_in = Field(default="header", const=True, alias="in")
17+
param_in = Field(default=ParameterLocation.HEADER, const=True, alias="in")
1818

1919
class Config:
2020
extra = Extra.ignore

openapi_schema_pydantic/v3/v3_0_3/parameter.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import enum
12
from typing import Any, Dict, Optional, Union
23

34
from pydantic import BaseModel, Field, Extra
@@ -8,6 +9,15 @@
89
from .schema import Schema
910

1011

12+
class ParameterLocation(str, enum.Enum):
13+
"""The location of a given parameter."""
14+
15+
QUERY = "query"
16+
HEADER = "header"
17+
PATH = "path"
18+
COOKIE = "cookie"
19+
20+
1121
class Parameter(BaseModel):
1222
"""
1323
Describes a single operation parameter.
@@ -30,7 +40,7 @@ class Parameter(BaseModel):
3040
- For all other cases, the `name` corresponds to the parameter name used by the [`in`](#parameterIn) property.
3141
"""
3242

33-
param_in: str = Field(alias="in")
43+
param_in: ParameterLocation = Field(alias="in")
3444
"""
3545
**REQUIRED**. The location of the parameter. Possible values are `"query"`, `"header"`, `"path"` or `"cookie"`.
3646
"""

openapi_schema_pydantic/v3/v3_1_0/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from .path_item import PathItem
1818
from .operation import Operation
1919
from .external_documentation import ExternalDocumentation
20-
from .parameter import Parameter
20+
from .parameter import Parameter, ParameterLocation
2121
from .request_body import RequestBody
2222
from .media_type import MediaType
2323
from .encoding import Encoding

openapi_schema_pydantic/v3/v3_1_0/header.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pydantic import Extra, Field
22

3-
from .parameter import Parameter
3+
from .parameter import Parameter, ParameterLocation
44

55

66
class Header(Parameter):
@@ -14,7 +14,7 @@ class Header(Parameter):
1414
"""
1515

1616
name = Field(default="", const=True)
17-
param_in = Field(default="header", const=True, alias="in")
17+
param_in = Field(default=ParameterLocation.HEADER, const=True, alias="in")
1818

1919
class Config:
2020
extra = Extra.ignore

openapi_schema_pydantic/v3/v3_1_0/parameter.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Any, Dict, Optional, Union
2+
import enum
23

34
from pydantic import BaseModel, Field, Extra
45

@@ -8,6 +9,15 @@
89
from .schema import Schema
910

1011

12+
class ParameterLocation(str, enum.Enum):
13+
"""The location of a given parameter."""
14+
15+
QUERY = "query"
16+
HEADER = "header"
17+
PATH = "path"
18+
COOKIE = "cookie"
19+
20+
1121
class Parameter(BaseModel):
1222
"""
1323
Describes a single operation parameter.
@@ -30,7 +40,7 @@ class Parameter(BaseModel):
3040
- For all other cases, the `name` corresponds to the parameter name used by the [`in`](#parameterIn) property.
3141
"""
3242

33-
param_in: str = Field(alias="in")
43+
param_in: ParameterLocation = Field(alias="in")
3444
"""
3545
**REQUIRED**. The location of the parameter. Possible values are `"query"`, `"header"`, `"path"` or `"cookie"`.
3646
"""

tests/test_alias.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
from openapi_schema_pydantic import Header, MediaType, Parameter, PathItem, Reference, Schema, SecurityScheme
1+
from openapi_schema_pydantic import (
2+
Header,
3+
MediaType,
4+
Parameter,
5+
ParameterLocation,
6+
PathItem,
7+
Reference,
8+
Schema,
9+
SecurityScheme,
10+
)
211

312

413
def test_header_alias():
514
header_1 = Header(param_in="header")
615
header_2 = Header.parse_obj({"param_in": "header"})
716
header_3 = Header.parse_obj({"in": "header"})
8-
assert header_1 == header_2 == header_3
17+
header_4 = Header.parse_obj({"in": ParameterLocation.HEADER})
18+
assert header_1 == header_2 == header_3 == header_4
919

1020

1121
def test_media_type_alias():

0 commit comments

Comments
 (0)