forked from python-restx/flask-restx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fields_mask.py
895 lines (695 loc) · 28.5 KB
/
test_fields_mask.py
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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import json
import pytest
from collections import OrderedDict
from flask_restx import mask, Api, Resource, fields, marshal, Mask
def assert_data(tested, expected):
"""Compare data without caring about order and type (dict vs. OrderedDict)"""
tested = json.loads(json.dumps(tested))
expected = json.loads(json.dumps(expected))
assert tested == expected
class MaskMixin(object):
def test_empty_mask(self):
assert Mask("") == {}
def test_one_field(self):
assert Mask("field_name") == {"field_name": True}
def test_multiple_field(self):
mask = Mask("field1, field2, field3")
assert_data(mask, {"field1": True, "field2": True, "field3": True,})
def test_nested_fields(self):
parsed = Mask("nested{field1,field2}")
expected = {"nested": {"field1": True, "field2": True,}}
assert parsed == expected
def test_complex(self):
parsed = Mask("field1, nested{field, sub{subfield}}, field2")
expected = {
"field1": True,
"nested": {"field": True, "sub": {"subfield": True,}},
"field2": True,
}
assert_data(parsed, expected)
def test_star(self):
parsed = Mask("nested{field1,field2},*")
expected = {
"nested": {"field1": True, "field2": True,},
"*": True,
}
assert_data(parsed, expected)
def test_order(self):
parsed = Mask("f_3, nested{f_1, f_2, f_3}, f_2, f_1")
expected = OrderedDict(
[
("f_3", True),
("nested", OrderedDict([("f_1", True), ("f_2", True), ("f_3", True),])),
("f_2", True),
("f_1", True),
]
)
assert parsed == expected
def test_missing_closing_bracket(self):
with pytest.raises(mask.ParseError):
Mask("nested{")
def test_consecutive_coma(self):
with pytest.raises(mask.ParseError):
Mask("field,,")
def test_coma_before_bracket(self):
with pytest.raises(mask.ParseError):
Mask("field,{}")
def test_coma_after_bracket(self):
with pytest.raises(mask.ParseError):
Mask("nested{,}")
def test_unexpected_opening_bracket(self):
with pytest.raises(mask.ParseError):
Mask("{{field}}")
def test_unexpected_closing_bracket(self):
with pytest.raises(mask.ParseError):
Mask("{field}}")
def test_support_colons(self):
assert Mask("field:name") == {"field:name": True}
def test_support_dash(self):
assert Mask("field-name") == {"field-name": True}
def test_support_underscore(self):
assert Mask("field_name") == {"field_name": True}
class MaskUnwrappedTest(MaskMixin):
def parse(self, value):
return Mask(value)
class MaskWrappedTest(MaskMixin):
def parse(self, value):
return Mask("{" + value + "}")
class DObject(object):
"""A dead simple object built from a dictionnary (no recursion)"""
def __init__(self, data):
self.__dict__.update(data)
person_fields = {"name": fields.String, "age": fields.Integer}
class ApplyMaskTest(object):
def test_empty(self):
data = {
"integer": 42,
"string": "a string",
"boolean": True,
}
result = mask.apply(data, "{}")
assert result == {}
def test_single_field(self):
data = {
"integer": 42,
"string": "a string",
"boolean": True,
}
result = mask.apply(data, "{integer}")
assert result == {"integer": 42}
def test_multiple_fields(self):
data = {
"integer": 42,
"string": "a string",
"boolean": True,
}
result = mask.apply(data, "{integer, string}")
assert result == {"integer": 42, "string": "a string"}
def test_star_only(self):
data = {
"integer": 42,
"string": "a string",
"boolean": True,
}
result = mask.apply(data, "*")
assert result == data
def test_with_objects(self):
data = DObject({"integer": 42, "string": "a string", "boolean": True,})
result = mask.apply(data, "{integer, string}")
assert result == {"integer": 42, "string": "a string"}
def test_with_ordered_dict(self):
data = OrderedDict({"integer": 42, "string": "a string", "boolean": True,})
result = mask.apply(data, "{integer, string}")
assert result == {"integer": 42, "string": "a string"}
def test_nested_field(self):
data = {
"integer": 42,
"string": "a string",
"boolean": True,
"nested": {"integer": 42, "string": "a string", "boolean": True,},
}
result = mask.apply(data, "{nested}")
assert result == {
"nested": {"integer": 42, "string": "a string", "boolean": True,}
}
def test_nested_fields(self):
data = {"nested": {"integer": 42, "string": "a string", "boolean": True,}}
result = mask.apply(data, "{nested{integer}}")
assert result == {"nested": {"integer": 42}}
def test_nested_with_start(self):
data = {
"nested": {"integer": 42, "string": "a string", "boolean": True,},
"other": "value",
}
result = mask.apply(data, "{nested{integer},*}")
assert result == {"nested": {"integer": 42}, "other": "value"}
def test_nested_fields_when_none(self):
data = {"nested": None}
result = mask.apply(data, "{nested{integer}}")
assert result == {"nested": None}
def test_raw_api_fields(self):
family_fields = {
"father": fields.Raw,
"mother": fields.Raw,
}
result = mask.apply(family_fields, "father{name},mother{age}")
data = {
"father": {"name": "John", "age": 42},
"mother": {"name": "Jane", "age": 42},
}
expected = {"father": {"name": "John"}, "mother": {"age": 42}}
assert_data(marshal(data, result), expected)
# Should leave th original mask untouched
assert_data(marshal(data, family_fields), data)
def test_nested_api_fields(self):
family_fields = {
"father": fields.Nested(person_fields),
"mother": fields.Nested(person_fields),
}
result = mask.apply(family_fields, "father{name},mother{age}")
assert set(result.keys()) == set(["father", "mother"])
assert isinstance(result["father"], fields.Nested)
assert set(result["father"].nested.keys()) == set(["name"])
assert isinstance(result["mother"], fields.Nested)
assert set(result["mother"].nested.keys()) == set(["age"])
data = {
"father": {"name": "John", "age": 42},
"mother": {"name": "Jane", "age": 42},
}
expected = {"father": {"name": "John"}, "mother": {"age": 42}}
assert_data(marshal(data, result), expected)
# Should leave th original mask untouched
assert_data(marshal(data, family_fields), data)
def test_multiple_nested_api_fields(self):
level_2 = {"nested_2": fields.Nested(person_fields)}
level_1 = {"nested_1": fields.Nested(level_2)}
root = {"nested": fields.Nested(level_1)}
result = mask.apply(root, "nested{nested_1{nested_2{name}}}")
assert set(result.keys()) == set(["nested"])
assert isinstance(result["nested"], fields.Nested)
assert set(result["nested"].nested.keys()) == set(["nested_1"])
data = {"nested": {"nested_1": {"nested_2": {"name": "John", "age": 42}}}}
expected = {"nested": {"nested_1": {"nested_2": {"name": "John"}}}}
assert_data(marshal(data, result), expected)
# Should leave th original mask untouched
assert_data(marshal(data, root), data)
def test_list_fields_with_simple_field(self):
family_fields = {"name": fields.String, "members": fields.List(fields.String)}
result = mask.apply(family_fields, "members")
assert set(result.keys()) == set(["members"])
assert isinstance(result["members"], fields.List)
assert isinstance(result["members"].container, fields.String)
data = {"name": "Doe", "members": ["John", "Jane"]}
expected = {"members": ["John", "Jane"]}
assert_data(marshal(data, result), expected)
# Should leave th original mask untouched
assert_data(marshal(data, family_fields), data)
def test_list_fields_with_nested(self):
family_fields = {"members": fields.List(fields.Nested(person_fields))}
result = mask.apply(family_fields, "members{name}")
assert set(result.keys()) == set(["members"])
assert isinstance(result["members"], fields.List)
assert isinstance(result["members"].container, fields.Nested)
assert set(result["members"].container.nested.keys()) == set(["name"])
data = {"members": [{"name": "John", "age": 42}, {"name": "Jane", "age": 42},]}
expected = {"members": [{"name": "John"}, {"name": "Jane"}]}
assert_data(marshal(data, result), expected)
# Should leave th original mask untouched
assert_data(marshal(data, family_fields), data)
def test_list_fields_with_nested_inherited(self, app):
api = Api(app)
person = api.model("Person", {"name": fields.String, "age": fields.Integer})
child = api.inherit("Child", person, {"attr": fields.String})
family = api.model("Family", {"children": fields.List(fields.Nested(child))})
result = mask.apply(family.resolved, "children{name,attr}")
data = {
"children": [
{"name": "John", "age": 5, "attr": "value-john"},
{"name": "Jane", "age": 42, "attr": "value-jane"},
]
}
expected = {
"children": [
{"name": "John", "attr": "value-john"},
{"name": "Jane", "attr": "value-jane"},
]
}
assert_data(marshal(data, result), expected)
# Should leave th original mask untouched
assert_data(marshal(data, family), data)
def test_list_fields_with_raw(self):
family_fields = {"members": fields.List(fields.Raw)}
result = mask.apply(family_fields, "members{name}")
data = {"members": [{"name": "John", "age": 42}, {"name": "Jane", "age": 42},]}
expected = {"members": [{"name": "John"}, {"name": "Jane"}]}
assert_data(marshal(data, result), expected)
# Should leave th original mask untouched
assert_data(marshal(data, family_fields), data)
def test_list(self):
data = [
{"integer": 42, "string": "a string", "boolean": True,},
{"integer": 404, "string": "another string", "boolean": False,},
]
result = mask.apply(data, "{integer, string}")
assert result == [
{"integer": 42, "string": "a string"},
{"integer": 404, "string": "another string"},
]
def test_nested_list(self):
data = {
"integer": 42,
"list": [
{"integer": 42, "string": "a string",},
{"integer": 404, "string": "another string",},
],
}
result = mask.apply(data, "{list}")
assert result == {
"list": [
{"integer": 42, "string": "a string",},
{"integer": 404, "string": "another string",},
]
}
def test_nested_list_fields(self):
data = {
"list": [
{"integer": 42, "string": "a string",},
{"integer": 404, "string": "another string",},
]
}
result = mask.apply(data, "{list{integer}}")
assert result == {"list": [{"integer": 42}, {"integer": 404}]}
def test_missing_field_none_by_default(self):
result = mask.apply({}, "{integer}")
assert result == {"integer": None}
def test_missing_field_skipped(self):
result = mask.apply({}, "{integer}", skip=True)
assert result == {}
def test_missing_nested_field_skipped(self):
result = mask.apply({}, "nested{integer}", skip=True)
assert result == {}
def test_mask_error_on_simple_fields(self):
model = {
"name": fields.String,
}
with pytest.raises(mask.MaskError):
mask.apply(model, "name{notpossible}")
def test_mask_error_on_list_field(self):
model = {"nested": fields.List(fields.String)}
with pytest.raises(mask.MaskError):
mask.apply(model, "nested{notpossible}")
class MaskAPI(object):
def test_marshal_with_honour_field_mask_header(self, app, client):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
return {"name": "John Doe", "age": 42, "boolean": True}
data = client.get_json("/test/", headers={"X-Fields": "{name,age}"})
assert data == {"name": "John Doe", "age": 42}
def test_marshal_with_honour_field_mask_list(self, app, client):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
return [
{"name": "John Doe", "age": 42, "boolean": True},
{"name": "Jane Doe", "age": 33, "boolean": False},
]
data = client.get_json("/test/", headers={"X-Fields": "{name,age}"})
assert data == [
{"name": "John Doe", "age": 42,},
{"name": "Jane Doe", "age": 33,},
]
def test_marshal_with_honour_complex_field_mask_header(self, app, client):
api = Api(app)
person = api.model("Person", person_fields)
child = api.inherit("Child", person, {"attr": fields.String})
family = api.model(
"Family",
{
"father": fields.Nested(person),
"mother": fields.Nested(person),
"children": fields.List(fields.Nested(child)),
"free": fields.List(fields.Raw),
},
)
house = api.model(
"House", {"family": fields.Nested(family, attribute="people")}
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(house)
def get(self):
return {
"people": {
"father": {"name": "John", "age": 42},
"mother": {"name": "Jane", "age": 42},
"children": [
{"name": "Jack", "age": 5, "attr": "value-1"},
{"name": "Julie", "age": 7, "attr": "value-2"},
],
"free": [
{"key-1": "1-1", "key-2": "1-2"},
{"key-1": "2-1", "key-2": "2-2"},
],
}
}
data = client.get_json(
"/test/",
headers={
"X-Fields": "family{father{name},mother{age},children{name,attr},free{key-2}}"
},
)
assert data == {
"family": {
"father": {"name": "John"},
"mother": {"age": 42},
"children": [
{"name": "Jack", "attr": "value-1"},
{"name": "Julie", "attr": "value-2"},
],
"free": [{"key-2": "1-2"}, {"key-2": "2-2"}],
}
}
def test_marshal_honour_field_mask(self, app):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
)
data = {"name": "John Doe", "age": 42, "boolean": True}
result = api.marshal(data, model, mask="{name,age}")
assert result == {
"name": "John Doe",
"age": 42,
}
def test_marshal_with_honour_default_mask(self, app, client):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model, mask="{name,age}")
def get(self):
return {"name": "John Doe", "age": 42, "boolean": True}
data = self.get_json("/test/")
self.assertEqual(data, {"name": "John Doe", "age": 42,})
def test_marshal_with_honour_default_model_mask(self, app, client):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
mask="{name,age}",
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
return {"name": "John Doe", "age": 42, "boolean": True}
data = client.get_json("/test/")
assert data == {"name": "John Doe", "age": 42}
def test_marshal_with_honour_header_field_mask_with_default_model_mask(
self, app, client
):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
mask="{name,age}",
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
return {"name": "John Doe", "age": 42, "boolean": True}
data = client.get_json("/test/", headers={"X-Fields": "{name}"})
assert data == {"name": "John Doe"}
def test_marshal_with_honour_header_default_mask_with_default_model_mask(
self, app, client
):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
mask="{name,boolean}",
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model, mask="{name}")
def get(self):
return {"name": "John Doe", "age": 42, "boolean": True}
data = client.get_json("/test/")
assert data == {"name": "John Doe"}
def test_marshal_with_honour_header_field_mask_with_default_mask(self, app, client):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model, mask="{name,age}")
def get(self):
return {"name": "John Doe", "age": 42, "boolean": True}
data = client.get_json("/test/", headers={"X-Fields": "{name}"})
assert data == {"name": "John Doe"}
def test_marshal_with_honour_header_field_mask_with_default_mask_and_default_model_mask(
self, app, client
):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
mask="{name,boolean}",
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model, mask="{name,age}")
def get(self):
return {"name": "John Doe", "age": 42, "boolean": True}
data = client.get_json("/test/", headers={"X-Fields": "{name}"})
assert data == {"name": "John Doe"}
def test_marshal_with_honour_custom_field_mask(self, app, client):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
return {"name": "John Doe", "age": 42, "boolean": True}
app.config["RESTX_MASK_HEADER"] = "X-Mask"
data = client.get_json("/test/", headers={"X-Mask": "{name,age}"})
assert data == {"name": "John Doe", "age": 42}
def test_marshal_does_not_hit_unrequired_attributes(self, app, client):
api = Api(app)
model = api.model(
"Person",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
)
class Person(object):
def __init__(self, name, age):
self.name = name
self.age = age
@property
def boolean(self):
raise Exception()
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
return Person("John Doe", 42)
data = client.get_json("/test/", headers={"X-Fields": "{name,age}"})
assert data == {"name": "John Doe", "age": 42}
def test_marshal_with_skip_missing_fields(self, app, client):
api = Api(app)
model = api.model("Test", {"name": fields.String, "age": fields.Integer,})
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
return {
"name": "John Doe",
"age": 42,
}
data = client.get_json("/test/", headers={"X-Fields": "{name,missing}"})
assert data == {"name": "John Doe"}
def test_marshal_handle_inheritance(self, app):
api = Api(app)
person = api.model("Person", {"name": fields.String, "age": fields.Integer,})
child = api.inherit("Child", person, {"extra": fields.String,})
data = {"name": "John Doe", "age": 42, "extra": "extra"}
values = (
("name", {"name": "John Doe"}),
("name,extra", {"name": "John Doe", "extra": "extra"}),
("extra", {"extra": "extra"}),
)
for value, expected in values:
result = marshal(data, child, mask=value)
assert result == expected
def test_marshal_with_handle_polymorph(self, app, client):
api = Api(app)
parent = api.model("Person", {"name": fields.String,})
child1 = api.inherit("Child1", parent, {"extra1": fields.String,})
child2 = api.inherit("Child2", parent, {"extra2": fields.String,})
class Child1(object):
name = "child1"
extra1 = "extra1"
class Child2(object):
name = "child2"
extra2 = "extra2"
mapping = {Child1: child1, Child2: child2}
thing = api.model("Thing", {"owner": fields.Polymorph(mapping),})
@api.route("/thing-1/")
class Thing1Resource(Resource):
@api.marshal_with(thing)
def get(self):
return {"owner": Child1()}
@api.route("/thing-2/")
class Thing2Resource(Resource):
@api.marshal_with(thing)
def get(self):
return {"owner": Child2()}
data = client.get_json("/thing-1/", headers={"X-Fields": "owner{name}"})
assert data == {"owner": {"name": "child1"}}
data = client.get_json("/thing-1/", headers={"X-Fields": "owner{extra1}"})
assert data == {"owner": {"extra1": "extra1"}}
data = client.get_json("/thing-2/", headers={"X-Fields": "owner{name}"})
assert data == {"owner": {"name": "child2"}}
def test_raise_400_on_invalid_mask(self, app, client):
api = Api(app)
model = api.model("Test", {"name": fields.String, "age": fields.Integer,})
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
pass
response = client.get("/test/", headers={"X-Fields": "name{,missing}"})
assert response.status_code == 400
assert response.content_type == "application/json"
class SwaggerMaskHeaderTest(object):
def test_marshal_with_expose_mask_header(self, app, client):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
return {"name": "John Doe", "age": 42, "boolean": True}
specs = client.get_specs()
op = specs["paths"]["/test/"]["get"]
assert "parameters" in op
assert len(op["parameters"]) == 1
param = op["parameters"][0]
assert param["name"] == "X-Fields"
assert param["type"] == "string"
assert param["format"] == "mask"
assert param["in"] == "header"
assert "required" not in param
assert "default" not in param
def test_marshal_with_expose_custom_mask_header(self, app, client):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
return {"name": "John Doe", "age": 42, "boolean": True}
app.config["RESTX_MASK_HEADER"] = "X-Mask"
specs = client.get_specs()
op = specs["paths"]["/test/"]["get"]
assert "parameters" in op
assert len(op["parameters"]) == 1
param = op["parameters"][0]
assert param["name"] == "X-Mask"
def test_marshal_with_disabling_mask_header(self, app, client):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
return {"name": "John Doe", "age": 42, "boolean": True}
app.config["RESTX_MASK_SWAGGER"] = False
specs = client.get_specs()
op = specs["paths"]["/test/"]["get"]
assert "parameters" not in op
def test_is_only_exposed_on_marshal_with(self, app, client):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
)
@api.route("/test/")
class TestResource(Resource):
def get(self):
return api.marshal(
{"name": "John Doe", "age": 42, "boolean": True}, model
)
specs = client.get_specs()
op = specs["paths"]["/test/"]["get"]
assert "parameters" not in op
def test_marshal_with_expose_default_mask_header(self, app, client):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model, mask="{name,age}")
def get(self):
pass
specs = client.get_specs()
op = specs["paths"]["/test/"]["get"]
assert "parameters" in op
assert len(op["parameters"]) == 1
param = op["parameters"][0]
assert param["name"] == "X-Fields"
assert param["type"] == "string"
assert param["format"] == "mask"
assert param["default"] == "{name,age}"
assert param["in"] == "header"
assert "required" not in param
def test_marshal_with_expose_default_model_mask_header(self, app, client):
api = Api(app)
model = api.model(
"Test",
{"name": fields.String, "age": fields.Integer, "boolean": fields.Boolean,},
mask="{name,age}",
)
@api.route("/test/")
class TestResource(Resource):
@api.marshal_with(model)
def get(self):
pass
specs = client.get_specs()
definition = specs["definitions"]["Test"]
assert "x-mask" in definition
assert definition["x-mask"] == "{name,age}"