Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 84557a9

Browse files
authored
Adds tests of security schemas in endpoints (#68)
* Adds test of http basic auth * Remove comment * Fixes bearer test * Samples regenerated
1 parent 183a575 commit 84557a9

File tree

12 files changed

+295
-179
lines changed

12 files changed

+295
-179
lines changed

modules/openapi-json-schema-generator/src/main/resources/python/configuration.handlebars

+28-31
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,6 @@ class Configuration(object):
3737
The dict value is an API key prefix when generating the auth data.
3838
:param username: Username for HTTP basic authentication
3939
:param password: Password for HTTP basic authentication
40-
:param discard_unknown_keys: Boolean value indicating whether to discard
41-
unknown properties. A server may send a response that includes additional
42-
properties that are not known by the client in the following scenarios:
43-
1. The OpenAPI document is incomplete, i.e. it does not match the server
44-
implementation.
45-
2. The client was generated using an older version of the OpenAPI document
46-
and the server has been upgraded since then.
47-
If a schema in the OpenAPI document defines the additionalProperties attribute,
48-
then all undeclared properties received by the server are injected into the
49-
additional properties map. In that case, there are undeclared properties, and
50-
nothing to discard.
5140
:param disabled_client_side_validations (string): Comma-separated list of
5241
JSON schema validation keywords to disable JSON schema structural validation
5342
rules. The following keywords may be specified: multipleOf, maximum,
@@ -162,17 +151,29 @@ conf = {{{packageName}}}.Configuration(
162151

163152
_default = None
164153

165-
def __init__(self, host=None,
166-
api_key=None, api_key_prefix=None,
167-
username=None, password=None,
168-
discard_unknown_keys=False,
169-
disabled_client_side_validations="",
154+
def __init__(
155+
self,
156+
host=None,
157+
{{#if hasApiKeyMethods}}
158+
api_key=None,
159+
api_key_prefix=None,
160+
{{/if}}
161+
{{#if hasHttpBasicMethods}}
162+
username=None,
163+
password=None,
164+
{{/if}}
165+
disabled_client_side_validations="",
170166
{{#if hasHttpSignatureMethods}}
171-
signing_info=None,
167+
signing_info=None,
172168
{{/if}}
173-
server_index=None, server_variables=None,
174-
server_operation_index=None, server_operation_variables=None,
175-
):
169+
server_index=None,
170+
server_variables=None,
171+
server_operation_index=None,
172+
server_operation_variables=None,
173+
{{#or hasOAuthMethods hasBearerMethods}}
174+
access_token=None,
175+
{{/or}}
176+
):
176177
"""Constructor
177178
"""
178179
self._base_path = "{{{basePath}}}" if host is None else host
@@ -190,6 +191,7 @@ conf = {{{packageName}}}.Configuration(
190191
"""Temp file folder for downloading files
191192
"""
192193
# Authentication Settings
194+
{{#if hasApiKeyMethods}}
193195
self.api_key = {}
194196
if api_key:
195197
self.api_key = api_key
@@ -203,13 +205,15 @@ conf = {{{packageName}}}.Configuration(
203205
self.refresh_api_key_hook = None
204206
"""function hook to refresh API key if expired
205207
"""
208+
{{/if}}
209+
{{#if hasHttpBasicMethods}}
206210
self.username = username
207211
"""Username for HTTP basic authentication
208212
"""
209213
self.password = password
210214
"""Password for HTTP basic authentication
211215
"""
212-
self.discard_unknown_keys = discard_unknown_keys
216+
{{/if}}
213217
self.disabled_client_side_validations = disabled_client_side_validations
214218
{{#if hasHttpSignatureMethods}}
215219
if signing_info is not None:
@@ -218,18 +222,11 @@ conf = {{{packageName}}}.Configuration(
218222
"""The HTTP signing configuration
219223
"""
220224
{{/if}}
221-
{{#if hasOAuthMethods}}
222-
self.access_token = None
223-
"""access token for OAuth/Bearer
224-
"""
225-
{{/if}}
226-
{{#unless hasOAuthMethods}}
227-
{{#if hasBearerMethods}}
228-
self.access_token = None
225+
{{#or hasOAuthMethods hasBearerMethods}}
226+
self.access_token = access_token
229227
"""access token for OAuth/Bearer
230228
"""
231-
{{/if}}
232-
{{/unless}}
229+
{{/or}}
233230
self.logger = {}
234231
"""Logging Settings
235232
"""

modules/openapi-json-schema-generator/src/main/resources/python/doc_auth_partial.handlebars

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ configuration.api_key['{{{name}}}'] = 'YOUR_API_KEY'
101101

102102
# Configure OAuth2 access token for authorization: {{{name}}}
103103
configuration = {{{packageName}}}.Configuration(
104-
host = "{{{basePath}}}"
104+
host = "{{{basePath}}}",
105+
access_token = 'YOUR_ACCESS_TOKEN'
105106
)
106-
configuration.access_token = 'YOUR_ACCESS_TOKEN'
107107
{{/if}}
108108
{{/each}}
109109
{{/if}}

modules/openapi-json-schema-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml

+10-4
Original file line numberDiff line numberDiff line change
@@ -791,13 +791,16 @@ paths:
791791
description: Required String in group parameters
792792
required: true
793793
schema:
794-
type: integer
794+
type: string
795795
- name: required_boolean_group
796796
in: header
797797
description: Required Boolean in group parameters
798798
required: true
799799
schema:
800-
type: boolean
800+
type: string
801+
enum:
802+
- true
803+
- false
801804
- name: required_int64_group
802805
in: query
803806
description: Required Integer in group parameters
@@ -809,12 +812,15 @@ paths:
809812
in: query
810813
description: String in group parameters
811814
schema:
812-
type: integer
815+
type: string
813816
- name: boolean_group
814817
in: header
815818
description: Boolean in group parameters
816819
schema:
817-
type: boolean
820+
type: string
821+
enum:
822+
- true
823+
- false
818824
- name: int64_group
819825
in: query
820826
description: Integer in group parameters

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/configuration.py

+9-39
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ class Configuration(object):
4242
The dict value is an API key prefix when generating the auth data.
4343
:param username: Username for HTTP basic authentication
4444
:param password: Password for HTTP basic authentication
45-
:param discard_unknown_keys: Boolean value indicating whether to discard
46-
unknown properties. A server may send a response that includes additional
47-
properties that are not known by the client in the following scenarios:
48-
1. The OpenAPI document is incomplete, i.e. it does not match the server
49-
implementation.
50-
2. The client was generated using an older version of the OpenAPI document
51-
and the server has been upgraded since then.
52-
If a schema in the OpenAPI document defines the additionalProperties attribute,
53-
then all undeclared properties received by the server are injected into the
54-
additional properties map. In that case, there are undeclared properties, and
55-
nothing to discard.
5645
:param disabled_client_side_validations (string): Comma-separated list of
5746
JSON schema validation keywords to disable JSON schema structural validation
5847
rules. The following keywords may be specified: multipleOf, maximum,
@@ -80,14 +69,15 @@ class Configuration(object):
8069

8170
_default = None
8271

83-
def __init__(self, host=None,
84-
api_key=None, api_key_prefix=None,
85-
username=None, password=None,
86-
discard_unknown_keys=False,
87-
disabled_client_side_validations="",
88-
server_index=None, server_variables=None,
89-
server_operation_index=None, server_operation_variables=None,
90-
):
72+
def __init__(
73+
self,
74+
host=None,
75+
disabled_client_side_validations="",
76+
server_index=None,
77+
server_variables=None,
78+
server_operation_index=None,
79+
server_operation_variables=None,
80+
):
9181
"""Constructor
9282
"""
9383
self._base_path = "https://someserver.com/v1" if host is None else host
@@ -105,26 +95,6 @@ def __init__(self, host=None,
10595
"""Temp file folder for downloading files
10696
"""
10797
# Authentication Settings
108-
self.api_key = {}
109-
if api_key:
110-
self.api_key = api_key
111-
"""dict to store API key(s)
112-
"""
113-
self.api_key_prefix = {}
114-
if api_key_prefix:
115-
self.api_key_prefix = api_key_prefix
116-
"""dict to store API prefix (e.g. Bearer)
117-
"""
118-
self.refresh_api_key_hook = None
119-
"""function hook to refresh API key if expired
120-
"""
121-
self.username = username
122-
"""Username for HTTP basic authentication
123-
"""
124-
self.password = password
125-
"""Password for HTTP basic authentication
126-
"""
127-
self.discard_unknown_keys = discard_unknown_keys
12898
self.disabled_client_side_validations = disabled_client_side_validations
12999
self.logger = {}
130100
"""Logging Settings

samples/openapi3/client/features/nonCompliantUseDiscriminatorIfCompositionFails/python/this_package/configuration.py

+9-39
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ class Configuration(object):
4242
The dict value is an API key prefix when generating the auth data.
4343
:param username: Username for HTTP basic authentication
4444
:param password: Password for HTTP basic authentication
45-
:param discard_unknown_keys: Boolean value indicating whether to discard
46-
unknown properties. A server may send a response that includes additional
47-
properties that are not known by the client in the following scenarios:
48-
1. The OpenAPI document is incomplete, i.e. it does not match the server
49-
implementation.
50-
2. The client was generated using an older version of the OpenAPI document
51-
and the server has been upgraded since then.
52-
If a schema in the OpenAPI document defines the additionalProperties attribute,
53-
then all undeclared properties received by the server are injected into the
54-
additional properties map. In that case, there are undeclared properties, and
55-
nothing to discard.
5645
:param disabled_client_side_validations (string): Comma-separated list of
5746
JSON schema validation keywords to disable JSON schema structural validation
5847
rules. The following keywords may be specified: multipleOf, maximum,
@@ -80,14 +69,15 @@ class Configuration(object):
8069

8170
_default = None
8271

83-
def __init__(self, host=None,
84-
api_key=None, api_key_prefix=None,
85-
username=None, password=None,
86-
discard_unknown_keys=False,
87-
disabled_client_side_validations="",
88-
server_index=None, server_variables=None,
89-
server_operation_index=None, server_operation_variables=None,
90-
):
72+
def __init__(
73+
self,
74+
host=None,
75+
disabled_client_side_validations="",
76+
server_index=None,
77+
server_variables=None,
78+
server_operation_index=None,
79+
server_operation_variables=None,
80+
):
9181
"""Constructor
9282
"""
9383
self._base_path = "http://localhost:3000" if host is None else host
@@ -105,26 +95,6 @@ def __init__(self, host=None,
10595
"""Temp file folder for downloading files
10696
"""
10797
# Authentication Settings
108-
self.api_key = {}
109-
if api_key:
110-
self.api_key = api_key
111-
"""dict to store API key(s)
112-
"""
113-
self.api_key_prefix = {}
114-
if api_key_prefix:
115-
self.api_key_prefix = api_key_prefix
116-
"""dict to store API prefix (e.g. Bearer)
117-
"""
118-
self.refresh_api_key_hook = None
119-
"""function hook to refresh API key if expired
120-
"""
121-
self.username = username
122-
"""Username for HTTP basic authentication
123-
"""
124-
self.password = password
125-
"""Password for HTTP basic authentication
126-
"""
127-
self.discard_unknown_keys = discard_unknown_keys
12898
self.disabled_client_side_validations = disabled_client_side_validations
12999
self.logger = {}
130100
"""Logging Settings

samples/openapi3/client/petstore/python/docs/apis/tags/FakeApi.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -1328,11 +1328,11 @@ with petstore_api.ApiClient(configuration) as api_client:
13281328

13291329
# example passing only required values which don't have defaults set
13301330
query_params = {
1331-
'required_string_group': 1,
1331+
'required_string_group': "required_string_group_example",
13321332
'required_int64_group': 1,
13331333
}
13341334
header_params = {
1335-
'required_boolean_group': True,
1335+
'required_boolean_group': "true",
13361336
}
13371337
try:
13381338
# Fake endpoint to test group parameters (optional)
@@ -1345,14 +1345,14 @@ with petstore_api.ApiClient(configuration) as api_client:
13451345

13461346
# example passing only optional values
13471347
query_params = {
1348-
'required_string_group': 1,
1348+
'required_string_group': "required_string_group_example",
13491349
'required_int64_group': 1,
1350-
'string_group': 1,
1350+
'string_group': "string_group_example",
13511351
'int64_group': 1,
13521352
}
13531353
header_params = {
1354-
'required_boolean_group': True,
1355-
'boolean_group': True,
1354+
'required_boolean_group': "true",
1355+
'boolean_group': "true",
13561356
}
13571357
try:
13581358
# Fake endpoint to test group parameters (optional)
@@ -1389,7 +1389,7 @@ int64_group | Int64GroupSchema | | optional
13891389
## Model Type Info
13901390
Input Type | Accessed Type | Description | Notes
13911391
------------ | ------------- | ------------- | -------------
1392-
decimal.Decimal, int, | decimal.Decimal, | |
1392+
str, | str, | |
13931393

13941394
# RequiredInt64GroupSchema
13951395

@@ -1403,7 +1403,7 @@ decimal.Decimal, int, | decimal.Decimal, | | value must be a 64 bit integer
14031403
## Model Type Info
14041404
Input Type | Accessed Type | Description | Notes
14051405
------------ | ------------- | ------------- | -------------
1406-
decimal.Decimal, int, | decimal.Decimal, | |
1406+
str, | str, | |
14071407

14081408
# Int64GroupSchema
14091409

@@ -1425,14 +1425,14 @@ boolean_group | BooleanGroupSchema | | optional
14251425
## Model Type Info
14261426
Input Type | Accessed Type | Description | Notes
14271427
------------ | ------------- | ------------- | -------------
1428-
bool, | BoolClass, | |
1428+
str, | str, | | must be one of ["true", "false", ]
14291429

14301430
# BooleanGroupSchema
14311431

14321432
## Model Type Info
14331433
Input Type | Accessed Type | Description | Notes
14341434
------------ | ------------- | ------------- | -------------
1435-
bool, | BoolClass, | |
1435+
str, | str, | | must be one of ["true", "false", ]
14361436

14371437
### Return Types, Responses
14381438

0 commit comments

Comments
 (0)