Skip to content

Commit 3950b2b

Browse files
authored
Merge pull request #10 from kuimono/5-update-url-field-types
Fix issue #5 for url types in `SecurityScheme`, `OAuthFlow`
2 parents 523fcf7 + 65eec31 commit 3950b2b

File tree

7 files changed

+55
-14
lines changed

7 files changed

+55
-14
lines changed

openapi_schema_pydantic/v3/v3_0_3/oauth_flow.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, Optional
1+
from typing import Dict, Optional, Union
22

33
from pydantic import AnyUrl, BaseModel, Extra
44

@@ -8,21 +8,21 @@ class OAuthFlow(BaseModel):
88
Configuration details for a supported OAuth Flow
99
"""
1010

11-
authorizationUrl: Optional[AnyUrl] = None
11+
authorizationUrl: Optional[Union[AnyUrl, str]] = None
1212
"""
1313
**REQUIRED** for `oauth2 ("implicit", "authorizationCode")`.
1414
The authorization URL to be used for this flow.
1515
This MUST be in the form of a URL.
1616
"""
1717

18-
tokenUrl: Optional[AnyUrl] = None
18+
tokenUrl: Optional[Union[AnyUrl, str]] = None
1919
"""
2020
**REQUIRED** for `oauth2 ("password", "clientCredentials", "authorizationCode")`.
2121
The token URL to be used for this flow.
2222
This MUST be in the form of a URL.
2323
"""
2424

25-
refreshUrl: Optional[AnyUrl] = None
25+
refreshUrl: Optional[Union[AnyUrl, str]] = None
2626
"""
2727
The URL to be used for obtaining refresh tokens. This MUST be in the form of a URL.
2828
"""
@@ -47,5 +47,11 @@ class Config:
4747
"tokenUrl": "https://example.com/api/oauth/token",
4848
"scopes": {"write:pets": "modify pets in your account", "read:pets": "read your pets"},
4949
},
50+
{
51+
"authorizationUrl": "/api/oauth/dialog", # issue #5: allow relative path
52+
"tokenUrl": "/api/oauth/token", # issue #5: allow relative path
53+
"refreshUrl": "/api/oauth/token", # issue #5: allow relative path
54+
"scopes": {"write:pets": "modify pets in your account", "read:pets": "read your pets"},
55+
},
5056
]
5157
}

openapi_schema_pydantic/v3/v3_0_3/security_scheme.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Optional, Union
22

33
from pydantic import AnyUrl, BaseModel, Extra, Field
44

@@ -32,7 +32,7 @@ class SecurityScheme(BaseModel):
3232
**REQUIRED** for `apiKey`. The name of the header, query or cookie parameter to be used.
3333
"""
3434

35-
security_scheme_in: Optional[str] = Field(alias="in")
35+
security_scheme_in: Optional[str] = Field(alias="in", default=None)
3636
"""
3737
**REQUIRED** for `apiKey`. The location of the API key. Valid values are `"query"`, `"header"` or `"cookie"`.
3838
"""
@@ -59,7 +59,7 @@ class SecurityScheme(BaseModel):
5959
**REQUIRED** for `oauth2`. An object containing configuration information for the flow types supported.
6060
"""
6161

62-
openIdConnectUrl: Optional[AnyUrl] = None
62+
openIdConnectUrl: Optional[Union[AnyUrl, str]] = None
6363
"""
6464
**REQUIRED** for `openIdConnect`. OpenId Connect URL to discover OAuth2 configuration values.
6565
This MUST be in the form of a URL.
@@ -82,5 +82,7 @@ class Config:
8282
}
8383
},
8484
},
85+
{"type": "openIdConnect", "openIdConnectUrl": "https://example.com/openIdConnect"},
86+
{"type": "openIdConnect", "openIdConnectUrl": "openIdConnect"}, # #5: allow relative path
8587
]
8688
}

openapi_schema_pydantic/v3/v3_1_0/oauth_flow.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, Optional
1+
from typing import Dict, Optional, Union
22

33
from pydantic import AnyUrl, BaseModel, Extra
44

@@ -8,23 +8,23 @@ class OAuthFlow(BaseModel):
88
Configuration details for a supported OAuth Flow
99
"""
1010

11-
authorizationUrl: Optional[AnyUrl] = None
11+
authorizationUrl: Optional[Union[AnyUrl, str]] = None
1212
"""
1313
**REQUIRED** for `oauth2 ("implicit", "authorizationCode")`.
1414
The authorization URL to be used for this flow.
1515
This MUST be in the form of a URL.
1616
The OAuth2 standard requires the use of TLS.
1717
"""
1818

19-
tokenUrl: Optional[AnyUrl] = None
19+
tokenUrl: Optional[Union[AnyUrl, str]] = None
2020
"""
2121
**REQUIRED** for `oauth2 ("password", "clientCredentials", "authorizationCode")`.
2222
The token URL to be used for this flow.
2323
This MUST be in the form of a URL.
2424
The OAuth2 standard requires the use of TLS.
2525
"""
2626

27-
refreshUrl: Optional[AnyUrl] = None
27+
refreshUrl: Optional[Union[AnyUrl, str]] = None
2828
"""
2929
The URL to be used for obtaining refresh tokens.
3030
This MUST be in the form of a URL.
@@ -51,5 +51,11 @@ class Config:
5151
"tokenUrl": "https://example.com/api/oauth/token",
5252
"scopes": {"write:pets": "modify pets in your account", "read:pets": "read your pets"},
5353
},
54+
{
55+
"authorizationUrl": "/api/oauth/dialog", # issue #5: allow relative path
56+
"tokenUrl": "/api/oauth/token", # issue #5: allow relative path
57+
"refreshUrl": "/api/oauth/token", # issue #5: allow relative path
58+
"scopes": {"write:pets": "modify pets in your account", "read:pets": "read your pets"},
59+
},
5460
]
5561
}

openapi_schema_pydantic/v3/v3_1_0/security_scheme.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Optional, Union
22

33
from pydantic import AnyUrl, BaseModel, Extra, Field
44

@@ -38,7 +38,7 @@ class SecurityScheme(BaseModel):
3838
**REQUIRED** for `apiKey`. The name of the header, query or cookie parameter to be used.
3939
"""
4040

41-
security_scheme_in: Optional[str] = Field(alias="in")
41+
security_scheme_in: Optional[str] = Field(alias="in", default=None)
4242
"""
4343
**REQUIRED** for `apiKey`. The location of the API key. Valid values are `"query"`, `"header"` or `"cookie"`.
4444
"""
@@ -65,7 +65,7 @@ class SecurityScheme(BaseModel):
6565
**REQUIRED** for `oauth2`. An object containing configuration information for the flow types supported.
6666
"""
6767

68-
openIdConnectUrl: Optional[AnyUrl] = None
68+
openIdConnectUrl: Optional[Union[AnyUrl, str]] = None
6969
"""
7070
**REQUIRED** for `openIdConnect`. OpenId Connect URL to discover OAuth2 configuration values.
7171
This MUST be in the form of a URL. The OpenID Connect standard requires the use of TLS.
@@ -88,5 +88,7 @@ class Config:
8888
}
8989
},
9090
},
91+
{"type": "openIdConnect", "openIdConnectUrl": "https://example.com/openIdConnect"},
92+
{"type": "openIdConnect", "openIdConnectUrl": "openIdConnect"}, # issue #5: allow relative path
9193
]
9294
}

tests/schema_classes/__init__.py

Whitespace-only changes.
File renamed without changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from pydantic import AnyUrl
2+
3+
from openapi_schema_pydantic import SecurityScheme
4+
5+
6+
def test_security_scheme_issue_5():
7+
"""https://github.com/kuimono/openapi-schema-pydantic/issues/5"""
8+
9+
security_scheme_1 = SecurityScheme(type="openIdConnect", openIdConnectUrl="https://example.com/openIdConnect")
10+
assert isinstance(security_scheme_1.openIdConnectUrl, AnyUrl)
11+
assert security_scheme_1.json(by_alias=True, exclude_none=True) == (
12+
'{"type": "openIdConnect", "openIdConnectUrl": "https://example.com/openIdConnect"}'
13+
)
14+
15+
security_scheme_2 = SecurityScheme(type="openIdConnect", openIdConnectUrl="/openIdConnect")
16+
assert isinstance(security_scheme_2.openIdConnectUrl, str)
17+
assert security_scheme_2.json(by_alias=True, exclude_none=True) == (
18+
'{"type": "openIdConnect", "openIdConnectUrl": "/openIdConnect"}'
19+
)
20+
21+
security_scheme_3 = SecurityScheme(type="openIdConnect", openIdConnectUrl="openIdConnect")
22+
assert isinstance(security_scheme_3.openIdConnectUrl, str)
23+
assert security_scheme_3.json(by_alias=True, exclude_none=True) == (
24+
'{"type": "openIdConnect", "openIdConnectUrl": "openIdConnect"}'
25+
)

0 commit comments

Comments
 (0)