1
+ from openapi_python_client .parser .properties .schemas import Parameters , ReferencePath
1
2
import openapi_python_client .schema as oai
2
3
from openapi_python_client import GeneratorError
3
4
from openapi_python_client .parser .errors import ParseError
@@ -23,7 +24,9 @@ def test_from_dict(self, mocker):
23
24
24
25
OpenAPI .parse_obj .assert_called_once_with (in_dict )
25
26
build_schemas .assert_called_once_with (components = openapi .components .schemas )
26
- EndpointCollection .from_data .assert_called_once_with (data = openapi .paths , schemas = build_schemas .return_value )
27
+ EndpointCollection .from_data .assert_called_once_with (
28
+ data = openapi .paths , schemas = build_schemas .return_value , parameters = Parameters ()
29
+ )
27
30
assert generator_data == GeneratorData (
28
31
title = openapi .info .title ,
29
32
description = openapi .info .description ,
@@ -374,7 +377,7 @@ def test__add_responses(self, mocker):
374
377
}
375
378
assert response_schemas == schemas_2
376
379
377
- def test__add_parameters_handles_no_params (self ):
380
+ def test__add_parameters_handles_no_params (self , mocker ):
378
381
from openapi_python_client .parser .openapi import Endpoint , Schemas
379
382
380
383
endpoint = Endpoint (
@@ -386,8 +389,11 @@ def test__add_parameters_handles_no_params(self):
386
389
tag = "tag" ,
387
390
)
388
391
schemas = Schemas ()
392
+ parameters = mocker .MagicMock ()
389
393
# Just checking there's no exception here
390
- assert Endpoint ._add_parameters (endpoint = endpoint , data = oai .Operation .construct (), schemas = schemas ) == (
394
+ assert Endpoint ._add_parameters (
395
+ endpoint = endpoint , data = oai .Operation .construct (), schemas = schemas , parameters = parameters
396
+ ) == (
391
397
endpoint ,
392
398
schemas ,
393
399
)
@@ -404,13 +410,17 @@ def test__add_parameters_parse_error(self, mocker):
404
410
tag = "tag" ,
405
411
)
406
412
initial_schemas = mocker .MagicMock ()
413
+ parameters = mocker .MagicMock ()
407
414
parse_error = ParseError (data = mocker .MagicMock ())
408
415
property_schemas = mocker .MagicMock ()
409
416
mocker .patch (f"{ MODULE_NAME } .property_from_data" , return_value = (parse_error , property_schemas ))
410
417
param = oai .Parameter .construct (name = "test" , required = True , param_schema = mocker .MagicMock (), param_in = "cookie" )
411
418
412
419
result = Endpoint ._add_parameters (
413
- endpoint = endpoint , data = oai .Operation .construct (parameters = [param ]), schemas = initial_schemas
420
+ endpoint = endpoint ,
421
+ data = oai .Operation .construct (parameters = [param ]),
422
+ schemas = initial_schemas ,
423
+ parameters = parameters ,
414
424
)
415
425
assert result == (
416
426
ParseError (data = parse_error .data , detail = f"cannot parse parameter of endpoint { endpoint .name } " ),
@@ -432,9 +442,10 @@ def test__add_parameters_fail_loudly_when_location_not_supported(self, mocker):
432
442
mocker .patch (f"{ MODULE_NAME } .property_from_data" , return_value = (mocker .MagicMock (), parsed_schemas ))
433
443
param = oai .Parameter .construct (name = "test" , required = True , param_schema = mocker .MagicMock (), param_in = "cookie" )
434
444
schemas = Schemas ()
445
+ parameters = mocker .MagicMock ()
435
446
436
447
result = Endpoint ._add_parameters (
437
- endpoint = endpoint , data = oai .Operation .construct (parameters = [param ]), schemas = schemas
448
+ endpoint = endpoint , data = oai .Operation .construct (parameters = [param ]), schemas = schemas , parameters = parameters
438
449
)
439
450
assert result == (ParseError (data = param , detail = "Parameter must be declared in path or query" ), parsed_schemas )
440
451
@@ -460,16 +471,26 @@ def test__add_parameters_happy(self, mocker):
460
471
header_prop = mocker .MagicMock (autospec = Property )
461
472
header_prop_import = mocker .MagicMock ()
462
473
header_prop .get_imports = mocker .MagicMock (return_value = {header_prop_import })
474
+ ref_path_prop = mocker .MagicMock (autospec = Property )
475
+ ref_path_prop_import = mocker .MagicMock ()
476
+ ref_path_prop .get_imports = mocker .MagicMock (return_value = {ref_path_prop_import })
463
477
schemas_1 = mocker .MagicMock ()
464
478
schemas_2 = mocker .MagicMock ()
465
479
schemas_3 = mocker .MagicMock ()
480
+ schemas_4 = mocker .MagicMock ()
466
481
property_from_data = mocker .patch (
467
482
f"{ MODULE_NAME } .property_from_data" ,
468
- side_effect = [(path_prop , schemas_1 ), (query_prop , schemas_2 ), (header_prop , schemas_3 )],
483
+ side_effect = [
484
+ (path_prop , schemas_1 ),
485
+ (query_prop , schemas_2 ),
486
+ (header_prop , schemas_3 ),
487
+ (ref_path_prop , schemas_4 ),
488
+ ],
469
489
)
470
490
path_schema = mocker .MagicMock ()
471
491
query_schema = mocker .MagicMock ()
472
492
header_schema = mocker .MagicMock ()
493
+ ref_path_schema = mocker .MagicMock ()
473
494
data = oai .Operation .construct (
474
495
parameters = [
475
496
oai .Parameter .construct (
@@ -481,13 +502,24 @@ def test__add_parameters_happy(self, mocker):
481
502
oai .Parameter .construct (
482
503
name = "header_prop_name" , required = False , param_schema = header_schema , param_in = "header"
483
504
),
484
- oai .Reference .construct (), # Should be ignored
485
- oai .Parameter .construct (), # Should be ignored
505
+ oai .Reference .construct (ref = "#/components/parameters/abc" ),
486
506
]
487
507
)
488
508
initial_schemas = mocker .MagicMock ()
509
+ parameters = Parameters (
510
+ classes_by_reference = {
511
+ ReferencePath ("/components/parameters/abc" ): oai .Parameter .construct (
512
+ name = "abc" , required = True , param_schema = ref_path_schema , param_in = "path"
513
+ ),
514
+ },
515
+ classes_by_name = {
516
+ "abc" : oai .Parameter .construct (name = "abc" , required = True , param_schema = path_schema , param_in = "path" ),
517
+ },
518
+ )
489
519
490
- (endpoint , schemas ) = Endpoint ._add_parameters (endpoint = endpoint , data = data , schemas = initial_schemas )
520
+ (endpoint , schemas ) = Endpoint ._add_parameters (
521
+ endpoint = endpoint , data = data , schemas = initial_schemas , parameters = parameters
522
+ )
491
523
492
524
property_from_data .assert_has_calls (
493
525
[
@@ -500,16 +532,26 @@ def test__add_parameters_happy(self, mocker):
500
532
mocker .call (
501
533
name = "header_prop_name" , required = False , data = header_schema , schemas = schemas_2 , parent_name = "name"
502
534
),
535
+ mocker .call (name = "abc" , required = True , data = ref_path_schema , schemas = schemas_3 , parent_name = "name" ),
503
536
]
504
537
)
538
+
505
539
path_prop .get_imports .assert_called_once_with (prefix = "..." )
506
540
query_prop .get_imports .assert_called_once_with (prefix = "..." )
507
541
header_prop .get_imports .assert_called_once_with (prefix = "..." )
508
- assert endpoint .relative_imports == {"import_3" , path_prop_import , query_prop_import , header_prop_import }
509
- assert endpoint .path_parameters == [path_prop ]
542
+ ref_path_prop .get_imports .assert_called_once_with (prefix = "..." )
543
+ print (f"goldstep '{ endpoint } '" )
544
+ assert endpoint .relative_imports == {
545
+ "import_3" ,
546
+ path_prop_import ,
547
+ query_prop_import ,
548
+ header_prop_import ,
549
+ ref_path_prop_import ,
550
+ }
551
+ assert endpoint .path_parameters == [path_prop , ref_path_prop ]
510
552
assert endpoint .query_parameters == [query_prop ]
511
553
assert endpoint .header_parameters == [header_prop ]
512
- assert schemas == schemas_3
554
+ assert schemas == schemas_4
513
555
514
556
def test_from_data_bad_params (self , mocker ):
515
557
from openapi_python_client .parser .openapi import Endpoint
@@ -526,8 +568,11 @@ def test_from_data_bad_params(self, mocker):
526
568
responses = mocker .MagicMock (),
527
569
)
528
570
inital_schemas = mocker .MagicMock ()
571
+ parameters = mocker .MagicMock ()
529
572
530
- result = Endpoint .from_data (data = data , path = path , method = method , tag = "default" , schemas = inital_schemas )
573
+ result = Endpoint .from_data (
574
+ data = data , path = path , method = method , tag = "default" , schemas = inital_schemas , parameters = parameters
575
+ )
531
576
532
577
assert result == (parse_error , return_schemas )
533
578
@@ -550,8 +595,11 @@ def test_from_data_bad_responses(self, mocker):
550
595
responses = mocker .MagicMock (),
551
596
)
552
597
initial_schemas = mocker .MagicMock ()
598
+ parameters = mocker .MagicMock ()
553
599
554
- result = Endpoint .from_data (data = data , path = path , method = method , tag = "default" , schemas = initial_schemas )
600
+ result = Endpoint .from_data (
601
+ data = data , path = path , method = method , tag = "default" , schemas = initial_schemas , parameters = parameters
602
+ )
555
603
556
604
assert result == (parse_error , response_schemas )
557
605
@@ -578,10 +626,13 @@ def test_from_data_standard(self, mocker):
578
626
responses = mocker .MagicMock (),
579
627
)
580
628
initial_schemas = mocker .MagicMock ()
629
+ parameters = mocker .MagicMock ()
581
630
582
631
mocker .patch ("openapi_python_client.utils.remove_string_escapes" , return_value = data .description )
583
632
584
- endpoint = Endpoint .from_data (data = data , path = path , method = method , tag = "default" , schemas = initial_schemas )
633
+ endpoint = Endpoint .from_data (
634
+ data = data , path = path , method = method , tag = "default" , schemas = initial_schemas , parameters = parameters
635
+ )
585
636
586
637
assert endpoint == _add_body .return_value
587
638
@@ -596,6 +647,7 @@ def test_from_data_standard(self, mocker):
596
647
),
597
648
data = data ,
598
649
schemas = initial_schemas ,
650
+ parameters = parameters ,
599
651
)
600
652
_add_responses .assert_called_once_with (endpoint = param_endpoint , data = data .responses , schemas = param_schemas )
601
653
_add_body .assert_called_once_with (endpoint = response_endpoint , data = data , schemas = response_schemas )
@@ -619,9 +671,12 @@ def test_from_data_no_operation_id(self, mocker):
619
671
responses = mocker .MagicMock (),
620
672
)
621
673
schemas = mocker .MagicMock ()
674
+ parameters = mocker .MagicMock ()
622
675
mocker .patch ("openapi_python_client.utils.remove_string_escapes" , return_value = data .description )
623
676
624
- result = Endpoint .from_data (data = data , path = path , method = method , tag = "default" , schemas = schemas )
677
+ result = Endpoint .from_data (
678
+ data = data , path = path , method = method , tag = "default" , schemas = schemas , parameters = parameters
679
+ )
625
680
626
681
assert result == _add_body .return_value
627
682
@@ -636,6 +691,7 @@ def test_from_data_no_operation_id(self, mocker):
636
691
),
637
692
data = data ,
638
693
schemas = schemas ,
694
+ parameters = parameters ,
639
695
)
640
696
_add_responses .assert_called_once_with (
641
697
endpoint = _add_parameters .return_value [0 ], data = data .responses , schemas = _add_parameters .return_value [1 ]
@@ -664,8 +720,9 @@ def test_from_data_no_security(self, mocker):
664
720
method = mocker .MagicMock ()
665
721
mocker .patch ("openapi_python_client.utils.remove_string_escapes" , return_value = data .description )
666
722
schemas = mocker .MagicMock ()
723
+ parameters = mocker .MagicMock ()
667
724
668
- Endpoint .from_data (data = data , path = path , method = method , tag = "a" , schemas = schemas )
725
+ Endpoint .from_data (data = data , path = path , method = method , tag = "a" , schemas = schemas , parameters = parameters )
669
726
670
727
_add_parameters .assert_called_once_with (
671
728
endpoint = Endpoint (
@@ -678,6 +735,7 @@ def test_from_data_no_security(self, mocker):
678
735
),
679
736
data = data ,
680
737
schemas = schemas ,
738
+ parameters = parameters ,
681
739
)
682
740
_add_responses .assert_called_once_with (
683
741
endpoint = _add_parameters .return_value [0 ], data = data .responses , schemas = _add_parameters .return_value [1 ]
@@ -731,14 +789,31 @@ def test_from_data(self, mocker):
731
789
side_effect = [(endpoint_1 , schemas_1 ), (endpoint_2 , schemas_2 ), (endpoint_3 , schemas_3 )],
732
790
)
733
791
schemas = mocker .MagicMock ()
792
+ parameters = mocker .MagicMock ()
734
793
735
- result = EndpointCollection .from_data (data = data , schemas = schemas )
794
+ result = EndpointCollection .from_data (data = data , schemas = schemas , parameters = parameters )
736
795
737
796
endpoint_from_data .assert_has_calls (
738
797
[
739
- mocker .call (data = path_1_put , path = "path_1" , method = "put" , tag = "default" , schemas = schemas ),
740
- mocker .call (data = path_1_post , path = "path_1" , method = "post" , tag = "tag_2" , schemas = schemas_1 ),
741
- mocker .call (data = path_2_get , path = "path_2" , method = "get" , tag = "default" , schemas = schemas_2 ),
798
+ mocker .call (
799
+ data = path_1_put , path = "path_1" , method = "put" , tag = "default" , schemas = schemas , parameters = parameters
800
+ ),
801
+ mocker .call (
802
+ data = path_1_post ,
803
+ path = "path_1" ,
804
+ method = "post" ,
805
+ tag = "tag_2" ,
806
+ schemas = schemas_1 ,
807
+ parameters = parameters ,
808
+ ),
809
+ mocker .call (
810
+ data = path_2_get ,
811
+ path = "path_2" ,
812
+ method = "get" ,
813
+ tag = "default" ,
814
+ schemas = schemas_2 ,
815
+ parameters = parameters ,
816
+ ),
742
817
],
743
818
)
744
819
assert result == (
@@ -772,14 +847,31 @@ def test_from_data_errors(self, mocker):
772
847
],
773
848
)
774
849
schemas = mocker .MagicMock ()
850
+ parameters = mocker .MagicMock ()
775
851
776
- result , result_schemas = EndpointCollection .from_data (data = data , schemas = schemas )
852
+ result , result_schemas = EndpointCollection .from_data (data = data , schemas = schemas , parameters = parameters )
777
853
778
854
endpoint_from_data .assert_has_calls (
779
855
[
780
- mocker .call (data = path_1_put , path = "path_1" , method = "put" , tag = "default" , schemas = schemas ),
781
- mocker .call (data = path_1_post , path = "path_1" , method = "post" , tag = "tag_2" , schemas = schemas_1 ),
782
- mocker .call (data = path_2_get , path = "path_2" , method = "get" , tag = "default" , schemas = schemas_2 ),
856
+ mocker .call (
857
+ data = path_1_put , path = "path_1" , method = "put" , tag = "default" , schemas = schemas , parameters = parameters
858
+ ),
859
+ mocker .call (
860
+ data = path_1_post ,
861
+ path = "path_1" ,
862
+ method = "post" ,
863
+ tag = "tag_2" ,
864
+ schemas = schemas_1 ,
865
+ parameters = parameters ,
866
+ ),
867
+ mocker .call (
868
+ data = path_2_get ,
869
+ path = "path_2" ,
870
+ method = "get" ,
871
+ tag = "default" ,
872
+ schemas = schemas_2 ,
873
+ parameters = parameters ,
874
+ ),
783
875
],
784
876
)
785
877
assert result ["default" ].parse_errors [0 ].data == "1"
0 commit comments