forked from openapi-generators/openapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_openapi.py
More file actions
1260 lines (1074 loc) · 50.9 KB
/
test_openapi.py
File metadata and controls
1260 lines (1074 loc) · 50.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from unittest.mock import MagicMock
import pydantic
import pytest
import openapi_python_client.schema as oai
from openapi_python_client import Config, GeneratorError
from openapi_python_client.parser.errors import ParseError
from openapi_python_client.parser.openapi import Endpoint, EndpointCollection
from openapi_python_client.parser.properties import IntProperty, Schemas
MODULE_NAME = "openapi_python_client.parser.openapi"
class TestGeneratorData:
def test_from_dict(self, mocker, model_property_factory, enum_property_factory):
from openapi_python_client.parser.properties import Schemas
build_schemas = mocker.patch(f"{MODULE_NAME}.build_schemas")
EndpointCollection = mocker.patch(f"{MODULE_NAME}.EndpointCollection")
schemas = mocker.MagicMock()
schemas.classes_by_name = {
"Model": model_property_factory(),
"Enum": enum_property_factory(),
}
endpoints_collections_by_tag = mocker.MagicMock()
EndpointCollection.from_data.return_value = (endpoints_collections_by_tag, schemas)
OpenAPI = mocker.patch(f"{MODULE_NAME}.oai.OpenAPI")
openapi = OpenAPI.parse_obj.return_value
openapi.openapi = mocker.MagicMock(major=3)
config = mocker.MagicMock()
in_dict = mocker.MagicMock()
from openapi_python_client.parser.openapi import GeneratorData
generator_data = GeneratorData.from_dict(in_dict, config=config)
OpenAPI.parse_obj.assert_called_once_with(in_dict)
build_schemas.assert_called_once_with(components=openapi.components.schemas, config=config, schemas=Schemas())
EndpointCollection.from_data.assert_called_once_with(
data=openapi.paths, schemas=build_schemas.return_value, config=config
)
assert generator_data.title == openapi.info.title
assert generator_data.description == openapi.info.description
assert generator_data.version == openapi.info.version
assert generator_data.endpoint_collections_by_tag == endpoints_collections_by_tag
assert generator_data.errors == schemas.errors
assert list(generator_data.models) == [schemas.classes_by_name["Model"]]
assert list(generator_data.enums) == [schemas.classes_by_name["Enum"]]
# Test no components
openapi.components = None
build_schemas.reset_mock()
GeneratorData.from_dict(in_dict, config=config)
build_schemas.assert_not_called()
def test_from_dict_invalid_schema(self, mocker):
Schemas = mocker.patch(f"{MODULE_NAME}.Schemas")
config = mocker.MagicMock()
in_dict = {}
from openapi_python_client.parser.openapi import GeneratorData
generator_data = GeneratorData.from_dict(in_dict, config=config)
assert generator_data == GeneratorError(
header="Failed to parse OpenAPI document",
detail=(
"3 validation errors for OpenAPI\n"
"info\n"
" field required (type=value_error.missing)\n"
"paths\n"
" field required (type=value_error.missing)\n"
"openapi\n"
" field required (type=value_error.missing)"
),
)
Schemas.build.assert_not_called()
Schemas.assert_not_called()
def test_swagger_document_invalid_schema(self, mocker):
Schemas = mocker.patch(f"{MODULE_NAME}.Schemas")
config = mocker.MagicMock()
in_dict = {"swagger": "2.0"}
from openapi_python_client.parser.openapi import GeneratorData
generator_data = GeneratorData.from_dict(in_dict, config=config)
assert generator_data == GeneratorError(
header="Failed to parse OpenAPI document",
detail=(
"You may be trying to use a Swagger document; this is not supported by this project.\n\n"
"3 validation errors for OpenAPI\n"
"info\n"
" field required (type=value_error.missing)\n"
"paths\n"
" field required (type=value_error.missing)\n"
"openapi\n"
" field required (type=value_error.missing)"
),
)
Schemas.build.assert_not_called()
Schemas.assert_not_called()
class TestEndpoint:
def make_endpoint(self):
from openapi_python_client.parser.openapi import Endpoint
return Endpoint(
path="path",
method="method",
description=None,
name="name",
requires_security=False,
tag="tag",
relative_imports={"import_3"},
)
def test_parse_request_form_body(self, mocker):
ref = mocker.MagicMock()
body = oai.RequestBody.construct(
content={
"application/x-www-form-urlencoded": oai.MediaType.construct(
media_type_schema=oai.Reference.construct(ref=ref)
)
}
)
from_string = mocker.patch(f"{MODULE_NAME}.Class.from_string")
config = mocker.MagicMock()
from openapi_python_client.parser.openapi import Endpoint
result = Endpoint.parse_request_form_body(body=body, config=config)
from_string.assert_called_once_with(string=ref, config=config)
assert result == from_string.return_value
def test_parse_request_form_body_no_data(self):
body = oai.RequestBody.construct(content={})
config = MagicMock()
from openapi_python_client.parser.openapi import Endpoint
result = Endpoint.parse_request_form_body(body=body, config=config)
assert result is None
def test_parse_multipart_body(self, mocker, model_property_factory):
from openapi_python_client.parser.openapi import Endpoint, Schemas
from openapi_python_client.parser.properties import Class
class_info = Class(name="class_name", module_name="module_name")
prop_before = model_property_factory(class_info=class_info, is_multipart_body=False)
schema = mocker.MagicMock()
body = oai.RequestBody.construct(
content={"multipart/form-data": oai.MediaType.construct(media_type_schema=schema)}
)
schemas_before = Schemas()
config = MagicMock()
property_from_data = mocker.patch(
f"{MODULE_NAME}.property_from_data", return_value=(prop_before, schemas_before)
)
result = Endpoint.parse_multipart_body(body=body, schemas=schemas_before, parent_name="parent", config=config)
property_from_data.assert_called_once_with(
name="multipart_data",
required=True,
data=schema,
schemas=schemas_before,
parent_name="parent",
config=config,
)
prop_after = model_property_factory(class_info=class_info, is_multipart_body=True)
schemas_after = Schemas(classes_by_name={class_info.name: prop_after})
assert result == (prop_after, schemas_after)
def test_parse_multipart_body_existing_schema(self, mocker, model_property_factory):
from openapi_python_client.parser.openapi import Endpoint, Schemas
from openapi_python_client.parser.properties import Class
class_info = Class(name="class_name", module_name="module_name")
prop_before = model_property_factory(class_info=class_info, is_multipart_body=False)
schemas_before = Schemas(classes_by_name={class_info.name: prop_before})
schema = mocker.MagicMock()
body = oai.RequestBody.construct(
content={"multipart/form-data": oai.MediaType.construct(media_type_schema=schema)}
)
config = MagicMock()
property_from_data = mocker.patch(
f"{MODULE_NAME}.property_from_data", return_value=(prop_before, schemas_before)
)
result = Endpoint.parse_multipart_body(body=body, schemas=schemas_before, parent_name="parent", config=config)
property_from_data.assert_called_once_with(
name="multipart_data",
required=True,
data=schema,
schemas=schemas_before,
parent_name="parent",
config=config,
)
prop_after = model_property_factory(class_info=class_info, is_multipart_body=True)
schemas_after = Schemas(classes_by_name={class_info.name: prop_after})
assert result == (prop_after, schemas_after)
def test_parse_multipart_body_no_data(self):
from openapi_python_client.parser.openapi import Endpoint, Schemas
body = oai.RequestBody.construct(content={})
schemas = Schemas()
prop, schemas = Endpoint.parse_multipart_body(
body=body, schemas=schemas, parent_name="parent", config=MagicMock()
)
assert prop is None
def test_parse_request_json_body(self, mocker):
from openapi_python_client.parser.openapi import Endpoint, Schemas
schema = mocker.MagicMock()
body = oai.RequestBody.construct(
content={"application/json": oai.MediaType.construct(media_type_schema=schema)}
)
property_from_data = mocker.patch(f"{MODULE_NAME}.property_from_data")
schemas = Schemas()
config = MagicMock()
result = Endpoint.parse_request_json_body(body=body, schemas=schemas, parent_name="parent", config=config)
property_from_data.assert_called_once_with(
name="json_body", required=True, data=schema, schemas=schemas, parent_name="parent", config=config
)
assert result == property_from_data.return_value
def test_parse_request_json_body_no_data(self):
from openapi_python_client.parser.openapi import Endpoint, Schemas
body = oai.RequestBody.construct(content={})
schemas = Schemas()
result = Endpoint.parse_request_json_body(body=body, schemas=schemas, parent_name="parent", config=MagicMock())
assert result == (None, schemas)
def test_add_body_no_data(self, mocker):
from openapi_python_client.parser.openapi import Endpoint, Schemas
parse_request_form_body = mocker.patch.object(Endpoint, "parse_request_form_body")
endpoint = self.make_endpoint()
schemas = Schemas()
Endpoint._add_body(endpoint=endpoint, data=oai.Operation.construct(), schemas=schemas, config=MagicMock())
parse_request_form_body.assert_not_called()
def test_add_body_bad_json_data(self, mocker):
from openapi_python_client.parser.openapi import Endpoint, Schemas
mocker.patch.object(Endpoint, "parse_request_form_body")
parse_error = ParseError(data=mocker.MagicMock(), detail=mocker.MagicMock())
other_schemas = mocker.MagicMock()
mocker.patch.object(Endpoint, "parse_request_json_body", return_value=(parse_error, other_schemas))
endpoint = self.make_endpoint()
request_body = mocker.MagicMock()
schemas = Schemas()
result = Endpoint._add_body(
endpoint=endpoint,
data=oai.Operation.construct(requestBody=request_body),
schemas=schemas,
config=MagicMock(),
)
assert result == (
ParseError(
header=f"Cannot parse JSON body of endpoint {endpoint.name}",
detail=parse_error.detail,
data=parse_error.data,
),
other_schemas,
)
def test_add_body_bad_multipart_data(self, mocker):
from openapi_python_client.parser.openapi import Endpoint, Schemas
mocker.patch.object(Endpoint, "parse_request_form_body")
mocker.patch.object(Endpoint, "parse_request_json_body", return_value=(mocker.MagicMock(), mocker.MagicMock()))
parse_error = ParseError(data=mocker.MagicMock(), detail=mocker.MagicMock())
other_schemas = mocker.MagicMock()
mocker.patch.object(Endpoint, "parse_multipart_body", return_value=(parse_error, other_schemas))
endpoint = self.make_endpoint()
request_body = mocker.MagicMock()
schemas = Schemas()
result = Endpoint._add_body(
endpoint=endpoint,
data=oai.Operation.construct(requestBody=request_body),
schemas=schemas,
config=MagicMock(),
)
assert result == (
ParseError(
header=f"Cannot parse multipart body of endpoint {endpoint.name}",
detail=parse_error.detail,
data=parse_error.data,
),
other_schemas,
)
def test_add_body_happy(self, mocker):
from openapi_python_client.parser.openapi import Class, Endpoint
from openapi_python_client.parser.properties import Property
request_body = mocker.MagicMock()
config = mocker.MagicMock()
form_body_class = Class(name="A", module_name="a")
parse_request_form_body = mocker.patch.object(Endpoint, "parse_request_form_body", return_value=form_body_class)
multipart_body = mocker.MagicMock(autospec=Property)
multipart_body_imports = mocker.MagicMock()
multipart_body.get_imports.return_value = {multipart_body_imports}
multipart_schemas = mocker.MagicMock()
parse_multipart_body = mocker.patch.object(
Endpoint, "parse_multipart_body", return_value=(multipart_body, multipart_schemas)
)
json_body = mocker.MagicMock(autospec=Property)
json_body_imports = mocker.MagicMock()
json_body.get_imports.return_value = {json_body_imports}
json_schemas = mocker.MagicMock()
parse_request_json_body = mocker.patch.object(
Endpoint, "parse_request_json_body", return_value=(json_body, json_schemas)
)
import_string_from_class = mocker.patch(f"{MODULE_NAME}.import_string_from_class", return_value="import_1")
endpoint = self.make_endpoint()
initial_schemas = mocker.MagicMock()
(endpoint, response_schemas) = Endpoint._add_body(
endpoint=endpoint,
data=oai.Operation.construct(requestBody=request_body),
schemas=initial_schemas,
config=config,
)
assert response_schemas == multipart_schemas
parse_request_form_body.assert_called_once_with(body=request_body, config=config)
parse_request_json_body.assert_called_once_with(
body=request_body, schemas=initial_schemas, parent_name="name", config=config
)
parse_multipart_body.assert_called_once_with(
body=request_body, schemas=json_schemas, parent_name="name", config=config
)
import_string_from_class.assert_called_once_with(form_body_class, prefix="...models")
json_body.get_imports.assert_called_once_with(prefix="...")
multipart_body.get_imports.assert_called_once_with(prefix="...")
assert endpoint.relative_imports == {"import_1", "import_3", json_body_imports, multipart_body_imports}
assert endpoint.json_body == json_body
assert endpoint.form_body_class == form_body_class
assert endpoint.multipart_body == multipart_body
def test__add_responses_status_code_error(self, mocker):
from openapi_python_client.parser.openapi import Endpoint, Schemas
schemas = Schemas()
response_1_data = mocker.MagicMock()
data = {
"not_a_number": response_1_data,
}
endpoint = self.make_endpoint()
parse_error = ParseError(data=mocker.MagicMock())
response_from_data = mocker.patch(f"{MODULE_NAME}.response_from_data", return_value=(parse_error, schemas))
config = MagicMock()
response, schemas = Endpoint._add_responses(endpoint=endpoint, data=data, schemas=schemas, config=config)
assert response.errors == [
ParseError(
detail=f"Invalid response status code not_a_number (not a number), response will be ommitted from generated client"
)
]
response_from_data.assert_not_called()
def test__add_responses_error(self, mocker):
from openapi_python_client.parser.openapi import Endpoint, Schemas
schemas = Schemas()
response_1_data = mocker.MagicMock()
response_2_data = mocker.MagicMock()
data = {
"200": response_1_data,
"404": response_2_data,
}
endpoint = self.make_endpoint()
parse_error = ParseError(data=mocker.MagicMock())
response_from_data = mocker.patch(f"{MODULE_NAME}.response_from_data", return_value=(parse_error, schemas))
config = MagicMock()
response, schemas = Endpoint._add_responses(endpoint=endpoint, data=data, schemas=schemas, config=config)
response_from_data.assert_has_calls(
[
mocker.call(status_code=200, data=response_1_data, schemas=schemas, parent_name="name", config=config),
mocker.call(status_code=404, data=response_2_data, schemas=schemas, parent_name="name", config=config),
]
)
assert response.errors == [
ParseError(
detail=f"Cannot parse response for status code 200, response will be ommitted from generated client",
data=parse_error.data,
),
ParseError(
detail=f"Cannot parse response for status code 404, response will be ommitted from generated client",
data=parse_error.data,
),
]
def test__add_responses(self, mocker, date_time_property_factory, date_property_factory):
from openapi_python_client.parser.openapi import Endpoint, Response
response_1_data = mocker.MagicMock()
response_2_data = mocker.MagicMock()
data = {
"200": response_1_data,
"404": response_2_data,
}
endpoint = self.make_endpoint()
schemas = mocker.MagicMock()
schemas_1 = mocker.MagicMock()
schemas_2 = mocker.MagicMock()
response_1 = Response(
status_code=200,
source="source",
prop=date_time_property_factory(name="datetime"),
)
response_2 = Response(
status_code=404,
source="source",
prop=date_property_factory(name="date"),
)
response_from_data = mocker.patch(
f"{MODULE_NAME}.response_from_data", side_effect=[(response_1, schemas_1), (response_2, schemas_2)]
)
config = MagicMock()
endpoint, response_schemas = Endpoint._add_responses(
endpoint=endpoint, data=data, schemas=schemas, config=config
)
response_from_data.assert_has_calls(
[
mocker.call(status_code=200, data=response_1_data, schemas=schemas, parent_name="name", config=config),
mocker.call(
status_code=404, data=response_2_data, schemas=schemas_1, parent_name="name", config=config
),
]
)
assert endpoint.responses == [response_1, response_2]
assert endpoint.relative_imports == {
"from dateutil.parser import isoparse",
"from typing import cast",
"import datetime",
"import_3",
}
assert response_schemas == schemas_2
def test_add_parameters_handles_no_params(self):
from openapi_python_client.parser.openapi import Endpoint, Schemas
endpoint = self.make_endpoint()
schemas = Schemas()
config = MagicMock()
# Just checking there's no exception here
assert Endpoint.add_parameters(
endpoint=endpoint, data=oai.Operation.construct(), schemas=schemas, config=config
) == (
endpoint,
schemas,
)
def test_add_parameters_parse_error(self, mocker):
from openapi_python_client.parser.openapi import Endpoint
endpoint = self.make_endpoint()
initial_schemas = mocker.MagicMock()
parse_error = ParseError(data=mocker.MagicMock())
property_schemas = mocker.MagicMock()
mocker.patch(f"{MODULE_NAME}.property_from_data", return_value=(parse_error, property_schemas))
param = oai.Parameter.construct(name="test", required=True, param_schema=mocker.MagicMock(), param_in="cookie")
config = MagicMock()
result = Endpoint.add_parameters(
endpoint=endpoint, data=oai.Operation.construct(parameters=[param]), schemas=initial_schemas, config=config
)
assert result == (
ParseError(data=parse_error.data, detail=f"cannot parse parameter of endpoint {endpoint.name}"),
initial_schemas,
)
@pytest.mark.parametrize(
"data_type, allowed",
[
(oai.DataType.STRING, True),
(oai.DataType.INTEGER, True),
(oai.DataType.NUMBER, True),
(oai.DataType.BOOLEAN, True),
(oai.DataType.ARRAY, False),
(oai.DataType.OBJECT, False),
],
)
def test_add_parameters_header_types(self, data_type, allowed):
from openapi_python_client.parser.openapi import Endpoint
endpoint = self.make_endpoint()
initial_schemas = Schemas()
param = oai.Parameter.construct(
name="test", required=True, param_schema=oai.Schema(type=data_type), param_in=oai.ParameterLocation.HEADER
)
config = Config()
result = Endpoint.add_parameters(
endpoint=endpoint, data=oai.Operation.construct(parameters=[param]), schemas=initial_schemas, config=config
)
if allowed:
assert isinstance(result[0], Endpoint)
else:
assert isinstance(result[0], ParseError)
def test__add_parameters_parse_error_on_non_required_path_param(self):
endpoint = self.make_endpoint()
param = oai.Parameter.construct(
name="test",
required=False,
param_schema=oai.Schema.construct(type="string"),
param_in=oai.ParameterLocation.PATH,
)
schemas = Schemas()
result = Endpoint.add_parameters(
endpoint=endpoint, data=oai.Operation.construct(parameters=[param]), schemas=schemas, config=Config()
)
assert result == (ParseError(data=param, detail="Path parameter must be required"), schemas)
def test_validation_error_when_location_not_supported(self, mocker):
parsed_schemas = mocker.MagicMock()
mocker.patch(f"{MODULE_NAME}.property_from_data", return_value=(mocker.MagicMock(), parsed_schemas))
with pytest.raises(pydantic.ValidationError):
oai.Parameter(name="test", required=True, param_schema=mocker.MagicMock(), param_in="error_location")
def test__add_parameters_with_location_postfix_conflict1(self, mocker, property_factory):
"""Checks when the PythonIdentifier of new parameter already used."""
from openapi_python_client.parser.openapi import Endpoint
endpoint = self.make_endpoint()
path_prop_conflicted = property_factory(name="prop_name_path", required=True, nullable=False, default=None)
query_prop = property_factory(name="prop_name", required=True, nullable=False, default=None)
path_prop = property_factory(name="prop_name", required=True, nullable=False, default=None)
schemas_1 = mocker.MagicMock()
schemas_2 = mocker.MagicMock()
schemas_3 = mocker.MagicMock()
mocker.patch(
f"{MODULE_NAME}.property_from_data",
side_effect=[
(path_prop_conflicted, schemas_1),
(query_prop, schemas_2),
(path_prop, schemas_3),
],
)
path_conflicted_schema = mocker.MagicMock()
query_schema = mocker.MagicMock()
path_schema = mocker.MagicMock()
data = oai.Operation.construct(
parameters=[
oai.Parameter.construct(
name=path_prop_conflicted.name, required=True, param_schema=path_conflicted_schema, param_in="path"
),
oai.Parameter.construct(
name=query_prop.name, required=False, param_schema=query_schema, param_in="query"
),
oai.Parameter.construct(name=path_prop.name, required=True, param_schema=path_schema, param_in="path"),
oai.Reference.construct(), # Should be ignored
oai.Parameter.construct(), # Should be ignored
]
)
initial_schemas = mocker.MagicMock()
config = MagicMock()
result = Endpoint.add_parameters(endpoint=endpoint, data=data, schemas=initial_schemas, config=config)[0]
assert isinstance(result, ParseError)
assert result.detail == "Parameters with same Python identifier `prop_name_path` detected"
def test__add_parameters_with_location_postfix_conflict2(self, mocker, property_factory):
"""Checks when an existing parameter has a conflicting PythonIdentifier after renaming."""
from openapi_python_client.parser.openapi import Endpoint
endpoint = self.make_endpoint()
path_prop_conflicted = property_factory(name="prop_name_path", required=True, nullable=False, default=None)
path_prop = property_factory(name="prop_name", required=True, nullable=False, default=None)
query_prop = property_factory(name="prop_name", required=True, nullable=False, default=None)
schemas_1 = mocker.MagicMock()
schemas_2 = mocker.MagicMock()
schemas_3 = mocker.MagicMock()
property_from_data = mocker.patch(
f"{MODULE_NAME}.property_from_data",
side_effect=[
(path_prop_conflicted, schemas_1),
(path_prop, schemas_2),
(query_prop, schemas_3),
],
)
path_conflicted_schema = mocker.MagicMock()
path_schema = mocker.MagicMock()
query_schema = mocker.MagicMock()
data = oai.Operation.construct(
parameters=[
oai.Parameter.construct(
name=path_prop_conflicted.name, required=True, param_schema=path_conflicted_schema, param_in="path"
),
oai.Parameter.construct(name=path_prop.name, required=True, param_schema=path_schema, param_in="path"),
oai.Parameter.construct(
name=query_prop.name, required=False, param_schema=query_schema, param_in="query"
),
oai.Reference.construct(), # Should be ignored
oai.Parameter.construct(), # Should be ignored
]
)
initial_schemas = mocker.MagicMock()
config = MagicMock()
result = Endpoint.add_parameters(endpoint=endpoint, data=data, schemas=initial_schemas, config=config)[0]
assert isinstance(result, ParseError)
assert result.detail == "Parameters with same Python identifier `prop_name_path` detected"
def test__add_parameters_skips_references(self):
"""References are not supported as direct params yet"""
endpoint = self.make_endpoint()
data = oai.Operation.construct(
parameters=[
oai.Reference.construct(ref="blah"),
]
)
(endpoint, _) = endpoint.add_parameters(endpoint=endpoint, data=data, schemas=Schemas(), config=Config())
assert isinstance(endpoint, Endpoint)
assert (
len(endpoint.path_parameters)
+ len(endpoint.query_parameters)
+ len(endpoint.cookie_parameters)
+ len(endpoint.header_parameters)
== 0
)
def test__add_parameters_skips_params_without_schemas(self):
"""Params without schemas are allowed per spec, but the any type doesn't make sense as a parameter"""
endpoint = self.make_endpoint()
data = oai.Operation.construct(
parameters=[
oai.Parameter.construct(
name="param",
param_in="path",
),
]
)
(endpoint, _) = endpoint.add_parameters(endpoint=endpoint, data=data, schemas=Schemas(), config=Config())
assert isinstance(endpoint, Endpoint)
assert len(endpoint.path_parameters) == 0
def test__add_parameters_same_identifier_conflict(self):
endpoint = self.make_endpoint()
data = oai.Operation.construct(
parameters=[
oai.Parameter.construct(
name="param",
param_in="path",
param_schema=oai.Schema.construct(nullable=False, type="string"),
required=True,
),
oai.Parameter.construct(
name="param_path",
param_in="path",
param_schema=oai.Schema.construct(nullable=False, type="string"),
required=True,
),
oai.Parameter.construct(
name="param",
param_in="query",
param_schema=oai.Schema.construct(nullable=False, type="string"),
),
]
)
(err, _) = endpoint.add_parameters(endpoint=endpoint, data=data, schemas=Schemas(), config=Config())
assert isinstance(err, ParseError)
assert "param_path" in err.detail
def test__add_parameters_query_optionality(self):
endpoint = self.make_endpoint()
data = oai.Operation.construct(
parameters=[
oai.Parameter.construct(
name="not_null_not_required",
required=False,
param_schema=oai.Schema.construct(nullable=False, type="string"),
param_in="query",
),
oai.Parameter.construct(
name="not_null_required",
required=True,
param_schema=oai.Schema.construct(nullable=False, type="string"),
param_in="query",
),
oai.Parameter.construct(
name="null_not_required",
required=False,
param_schema=oai.Schema.construct(nullable=True, type="string"),
param_in="query",
),
oai.Parameter.construct(
name="null_required",
required=True,
param_schema=oai.Schema.construct(nullable=True, type="string"),
param_in="query",
),
]
)
(endpoint, _) = endpoint.add_parameters(endpoint=endpoint, data=data, schemas=Schemas(), config=Config())
assert len(endpoint.query_parameters) == 4, "Not all query params were added"
for param in endpoint.query_parameters.values():
if param.name == "not_null_required":
assert not param.nullable
assert param.required
else:
assert param.nullable
assert not param.required
def test_add_parameters_duplicate_properties(self):
from openapi_python_client.parser.openapi import Endpoint, Schemas
endpoint = self.make_endpoint()
param = oai.Parameter.construct(
name="test", required=True, param_schema=oai.Schema.construct(type="string"), param_in="path"
)
data = oai.Operation.construct(parameters=[param, param])
schemas = Schemas()
config = MagicMock()
result = Endpoint.add_parameters(endpoint=endpoint, data=data, schemas=schemas, config=config)
assert result == (
ParseError(
data=data,
detail="Parameters MUST NOT contain duplicates. "
"A unique parameter is defined by a combination of a name and location. "
"Duplicated parameters named `test` detected in `path`.",
),
schemas,
)
def test_add_parameters_duplicate_properties_different_location(self):
from openapi_python_client.parser.openapi import Endpoint, Schemas
endpoint = self.make_endpoint()
path_param = oai.Parameter.construct(
name="test", required=True, param_schema=oai.Schema.construct(type="string"), param_in="path"
)
query_param = oai.Parameter.construct(
name="test", required=True, param_schema=oai.Schema.construct(type="string"), param_in="query"
)
schemas = Schemas()
config = MagicMock()
result = Endpoint.add_parameters(
endpoint=endpoint,
data=oai.Operation.construct(parameters=[path_param, query_param]),
schemas=schemas,
config=config,
)[0]
assert isinstance(result, Endpoint)
assert result.path_parameters["test"].name == "test"
assert result.query_parameters["test"].name == "test"
def test_sort_parameters(self, string_property_factory):
from openapi_python_client.parser.openapi import Endpoint
endpoint = self.make_endpoint()
endpoint.path = "/multiple-path-parameters/{param4}/{param2}/{param1}/{param3}"
for i in range(1, 5):
prop = string_property_factory(name=f"param{i}")
endpoint.path_parameters[prop.name] = prop
result = Endpoint.sort_parameters(endpoint=endpoint)
result_names = [name for name in result.path_parameters]
expected_names = [f"param{i}" for i in (4, 2, 1, 3)]
assert result_names == expected_names
def test_sort_parameters_missing_param(self, string_property_factory):
from openapi_python_client.parser.openapi import Endpoint
endpoint = self.make_endpoint()
endpoint.path = "/multiple-path-parameters/{param1}/{param2}"
param = string_property_factory(name="param1")
endpoint.path_parameters[param.name] = param
result = Endpoint.sort_parameters(endpoint=endpoint)
assert isinstance(result, ParseError)
assert "Incorrect path templating" in result.detail
assert endpoint.path in result.detail
def test_sort_parameters_extra_param(self, string_property_factory):
from openapi_python_client.parser.openapi import Endpoint
endpoint = self.make_endpoint()
endpoint.path = "/multiple-path-parameters"
param = string_property_factory(name="param1")
endpoint.path_parameters[param.name] = param
result = Endpoint.sort_parameters(endpoint=endpoint)
assert isinstance(result, ParseError)
assert "Incorrect path templating" in result.detail
assert endpoint.path in result.detail
def test_from_data_bad_params(self, mocker):
from openapi_python_client.parser.openapi import Endpoint
path = mocker.MagicMock()
method = mocker.MagicMock()
parse_error = ParseError(data=mocker.MagicMock())
return_schemas = mocker.MagicMock()
add_parameters = mocker.patch.object(Endpoint, "add_parameters", return_value=(parse_error, return_schemas))
data = oai.Operation.construct(
description=mocker.MagicMock(),
operationId=mocker.MagicMock(),
security={"blah": "bloo"},
responses=mocker.MagicMock(),
)
inital_schemas = mocker.MagicMock()
config = MagicMock()
result = Endpoint.from_data(
data=data, path=path, method=method, tag="default", schemas=inital_schemas, config=config
)
assert result == (parse_error, return_schemas)
def test_from_data_bad_responses(self, mocker):
from openapi_python_client.parser.openapi import Endpoint
path = mocker.MagicMock()
method = mocker.MagicMock()
parse_error = ParseError(data=mocker.MagicMock())
param_schemas = mocker.MagicMock()
add_parameters = mocker.patch.object(
Endpoint, "add_parameters", return_value=(mocker.MagicMock(), param_schemas)
)
response_schemas = mocker.MagicMock()
_add_responses = mocker.patch.object(Endpoint, "_add_responses", return_value=(parse_error, response_schemas))
data = oai.Operation.construct(
description=mocker.MagicMock(),
operationId=mocker.MagicMock(),
security={"blah": "bloo"},
responses=mocker.MagicMock(),
)
initial_schemas = mocker.MagicMock()
config = MagicMock()
result = Endpoint.from_data(
data=data, path=path, method=method, tag="default", schemas=initial_schemas, config=config
)
assert result == (parse_error, response_schemas)
def test_from_data_standard(self, mocker):
from openapi_python_client.parser.openapi import Endpoint
path = mocker.MagicMock()
method = mocker.MagicMock()
param_schemas = mocker.MagicMock()
param_endpoint = mocker.MagicMock()
add_parameters = mocker.patch.object(Endpoint, "add_parameters", return_value=(param_endpoint, param_schemas))
response_schemas = mocker.MagicMock()
response_endpoint = mocker.MagicMock()
_add_responses = mocker.patch.object(
Endpoint, "_add_responses", return_value=(response_endpoint, response_schemas)
)
body_schemas = mocker.MagicMock()
body_endpoint = mocker.MagicMock()
_add_body = mocker.patch.object(Endpoint, "_add_body", return_value=(body_endpoint, body_schemas))
data = oai.Operation.construct(
description=mocker.MagicMock(),
operationId=mocker.MagicMock(),
security={"blah": "bloo"},
responses=mocker.MagicMock(),
)
initial_schemas = mocker.MagicMock()
config = MagicMock()
mocker.patch("openapi_python_client.utils.remove_string_escapes", return_value=data.description)
endpoint = Endpoint.from_data(
data=data, path=path, method=method, tag="default", schemas=initial_schemas, config=config
)
assert endpoint == _add_body.return_value
add_parameters.assert_called_once_with(
endpoint=Endpoint(
path=path,
method=method,
description=data.description,
summary="",
name=data.operationId,
requires_security=True,
tag="default",
),
data=data,
schemas=initial_schemas,
config=config,
)
_add_responses.assert_called_once_with(
endpoint=param_endpoint, data=data.responses, schemas=param_schemas, config=config
)
_add_body.assert_called_once_with(
endpoint=response_endpoint, data=data, schemas=response_schemas, config=config
)
def test_from_data_no_operation_id(self, mocker):
from openapi_python_client.parser.openapi import Endpoint
path = "/path/with/{param}/"
method = "get"
add_parameters = mocker.patch.object(
Endpoint, "add_parameters", return_value=(mocker.MagicMock(), mocker.MagicMock())
)
_add_responses = mocker.patch.object(
Endpoint, "_add_responses", return_value=(mocker.MagicMock(), mocker.MagicMock())
)
_add_body = mocker.patch.object(Endpoint, "_add_body", return_value=(mocker.MagicMock(), mocker.MagicMock()))
data = oai.Operation.construct(
description=mocker.MagicMock(),
operationId=None,
security={"blah": "bloo"},
responses=mocker.MagicMock(),
)
schemas = mocker.MagicMock()
mocker.patch("openapi_python_client.utils.remove_string_escapes", return_value=data.description)
config = MagicMock()
result = Endpoint.from_data(data=data, path=path, method=method, tag="default", schemas=schemas, config=config)
assert result == _add_body.return_value
add_parameters.assert_called_once_with(
endpoint=Endpoint(
path=path,
method=method,
description=data.description,
summary="",
name="get_path_with_param",
requires_security=True,
tag="default",
),
data=data,
schemas=schemas,
config=config,
)
_add_responses.assert_called_once_with(
endpoint=add_parameters.return_value[0],
data=data.responses,
schemas=add_parameters.return_value[1],
config=config,
)
_add_body.assert_called_once_with(
endpoint=_add_responses.return_value[0], data=data, schemas=_add_responses.return_value[1], config=config
)