Skip to content

Commit ff7c1f1

Browse files
committed
feat: Add parameter location enum.
1 parent 73c5995 commit ff7c1f1

File tree

7 files changed

+28
-10
lines changed

7 files changed

+28
-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: 10 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,14 @@
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+
1120
class Parameter(BaseModel):
1221
"""
1322
Describes a single operation parameter.
@@ -30,7 +39,7 @@ class Parameter(BaseModel):
3039
- For all other cases, the `name` corresponds to the parameter name used by the [`in`](#parameterIn) property.
3140
"""
3241

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

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: 9 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

@@ -7,6 +8,13 @@
78
from .reference import Reference
89
from .schema import Schema
910

11+
class ParameterLocation(str, enum.Enum):
12+
"""The location of a given parameter."""
13+
14+
QUERY = "query"
15+
HEADER = "header"
16+
PATH = "path"
17+
COOKIE = "cookie"
1018

1119
class Parameter(BaseModel):
1220
"""
@@ -30,7 +38,7 @@ class Parameter(BaseModel):
3038
- For all other cases, the `name` corresponds to the parameter name used by the [`in`](#parameterIn) property.
3139
"""
3240

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

tests/test_alias.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from openapi_schema_pydantic import Header, MediaType, Parameter, PathItem, Reference, Schema, SecurityScheme
1+
from openapi_schema_pydantic import Header, MediaType, Parameter, ParameterLocation, PathItem, Reference, Schema, SecurityScheme
22

33

44
def test_header_alias():
55
header_1 = Header(param_in="header")
66
header_2 = Header.parse_obj({"param_in": "header"})
77
header_3 = Header.parse_obj({"in": "header"})
8-
assert header_1 == header_2 == header_3
8+
header_4 = Header.parse_obj({"in": ParameterLocation.HEADER})
9+
assert header_1 == header_2 == header_3 == header_4
910

1011

1112
def test_media_type_alias():

0 commit comments

Comments
 (0)