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

Commit e791bd5

Browse files
authored
Adds prefixItems validation (#222)
* Adds java code to store prefixItems in codegenSchema * Adds two sample component schemas with prefixItems * Adds prefix_items data store in python schema class * Adds validate_prefix_items * Samples and docs regen * Samples and docs updated
1 parent bd48337 commit e791bd5

File tree

35 files changed

+549
-38
lines changed

35 files changed

+549
-38
lines changed

docs/generators/java.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
343343
|OneOf|✗|OAS3
344344
|Pattern|✓|OAS2,OAS3
345345
|PatternProperties|✗|OAS3
346+
|PrefixItems|✗|OAS3
346347
|Properties|✓|OAS2,OAS3
347348
|PropertyNames|✗|OAS3
348349
|Required|✓|OAS2,OAS3

docs/generators/jaxrs-jersey.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
326326
|OneOf|✗|OAS3
327327
|Pattern|✓|OAS2,OAS3
328328
|PatternProperties|✗|OAS3
329+
|PrefixItems|✗|OAS3
329330
|Properties|✓|OAS2,OAS3
330331
|PropertyNames|✗|OAS3
331332
|Required|✓|OAS2,OAS3

docs/generators/jmeter.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
185185
|OneOf|✗|OAS3
186186
|Pattern|✓|OAS2,OAS3
187187
|PatternProperties|✗|OAS3
188+
|PrefixItems|✗|OAS3
188189
|Properties|✓|OAS2,OAS3
189190
|PropertyNames|✗|OAS3
190191
|Required|✓|OAS2,OAS3

docs/generators/kotlin.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
295295
|OneOf|✗|OAS3
296296
|Pattern|✓|OAS2,OAS3
297297
|PatternProperties|✗|OAS3
298+
|PrefixItems|✗|OAS3
298299
|Properties|✓|OAS2,OAS3
299300
|PropertyNames|✗|OAS3
300301
|Required|✓|OAS2,OAS3

docs/generators/python.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
254254
|OneOf|✓|OAS3
255255
|Pattern|✓|OAS2,OAS3
256256
|PatternProperties|✓|OAS3
257+
|PrefixItems|✓|OAS3
257258
|Properties|✓|OAS2,OAS3
258259
|PropertyNames|✓|OAS3
259260
|Required|✓|OAS2,OAS3

samples/client/3_0_3_unit_test/python/src/unit_test_api/configurations/schema_configuration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
'not_': 'not',
4242
'one_of': 'oneOf',
4343
'pattern': 'pattern',
44+
'pattern_properties': 'patternProperties',
45+
'prefix_items': 'prefixItems',
4446
'properties': 'properties',
4547
'property_names': 'propertyNames',
4648
'required': 'required',

samples/client/3_0_3_unit_test/python/src/unit_test_api/schemas/validation.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,20 @@ def _validate(
101101
if 'contains' in vars(cls_schema):
102102
contains_qty = _get_contains_qty(
103103
arg,
104-
json_schema_data['contains'],
104+
vars(cls_schema)['contains'],
105105
validation_metadata,
106106
path_to_schemas
107107
)
108+
prefix_items_length = 0
109+
if 'prefix_items' in vars(cls_schema):
110+
prefix_items_length = len(vars(cls_schema)['prefix_items'])
108111
for keyword, val in json_schema_data.items():
109-
used_val = (val, contains_qty) if keyword in {'contains', 'min_contains', 'max_contains'} else val
112+
if keyword in {'contains', 'min_contains', 'max_contains'}:
113+
used_val = (val, contains_qty)
114+
elif keyword == 'items':
115+
used_val = (val, prefix_items_length)
116+
else:
117+
used_val = val
110118
validator = json_schema_keyword_to_validator[keyword]
111119

112120
other_path_to_schemas = validator(
@@ -660,15 +668,17 @@ def validate_required(
660668

661669
def validate_items(
662670
arg: typing.Any,
663-
item_cls: typing.Type[SchemaValidator],
671+
item_cls_prefix_items_length: typing.Tuple[typing.Type[SchemaValidator], int],
664672
cls: typing.Type,
665673
validation_metadata: ValidationMetadata,
666674
) -> typing.Optional[PathToSchemasType]:
667675
if not isinstance(arg, tuple):
668676
return None
669-
item_cls = _get_class(item_cls)
677+
item_cls = _get_class(item_cls_prefix_items_length[0])
678+
prefix_items_length = item_cls_prefix_items_length[1]
670679
path_to_schemas: PathToSchemasType = {}
671-
for i, value in enumerate(arg):
680+
for i in range(prefix_items_length, len(arg)):
681+
value = arg[i]
672682
item_validation_metadata = ValidationMetadata(
673683
path_to_item=validation_metadata.path_to_item+(i,),
674684
configuration=validation_metadata.configuration,
@@ -1154,6 +1164,34 @@ def validate_pattern_properties(
11541164
return path_to_schemas
11551165

11561166

1167+
def validate_prefix_items(
1168+
arg: typing.Any,
1169+
prefix_items: typing.Tuple[typing.Type[SchemaValidator], ...],
1170+
cls: typing.Type,
1171+
validation_metadata: ValidationMetadata,
1172+
) -> typing.Optional[PathToSchemasType]:
1173+
if not isinstance(arg, tuple):
1174+
return None
1175+
path_to_schemas: PathToSchemasType = {}
1176+
module_namespace = vars(sys.modules[cls.__module__])
1177+
for i, val in enumerate(arg):
1178+
if i >= len(prefix_items):
1179+
break
1180+
schema = _get_class(prefix_items[i], module_namespace)
1181+
path_to_item = validation_metadata.path_to_item + (i,)
1182+
item_validation_metadata = ValidationMetadata(
1183+
path_to_item=path_to_item,
1184+
configuration=validation_metadata.configuration,
1185+
validated_path_to_schemas=validation_metadata.validated_path_to_schemas
1186+
)
1187+
if item_validation_metadata.validation_ran_earlier(schema):
1188+
add_deeper_validated_schemas(validation_metadata, path_to_schemas)
1189+
continue
1190+
other_path_to_schemas = schema._validate(val, validation_metadata=item_validation_metadata)
1191+
update(path_to_schemas, other_path_to_schemas)
1192+
return path_to_schemas
1193+
1194+
11571195
validator_type = typing.Callable[[typing.Any, typing.Any, type, ValidationMetadata], typing.Optional[PathToSchemasType]]
11581196
json_schema_keyword_to_validator: typing.Mapping[str, validator_type] = {
11591197
'types': validate_types,
@@ -1188,5 +1226,6 @@ def validate_pattern_properties(
11881226
'dependent_required': validate_dependent_required,
11891227
'dependent_schemas': validate_dependent_schemas,
11901228
'property_names': validate_property_names,
1191-
'pattern_properties': validate_pattern_properties
1229+
'pattern_properties': validate_pattern_properties,
1230+
'prefix_items': validate_prefix_items
11921231
}

samples/client/3_1_0_json_schema/python/.openapi-generator/FILES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ docs/components/schema/any_type_dependent_schemas.md
1010
docs/components/schema/any_type_max_contains_value.md
1111
docs/components/schema/any_type_min_contains_value.md
1212
docs/components/schema/any_type_pattern_properties.md
13+
docs/components/schema/any_type_prefix_items.md
1314
docs/components/schema/any_type_property_names.md
1415
docs/components/schema/array_contains_value.md
1516
docs/components/schema/array_max_contains_value.md
1617
docs/components/schema/array_min_contains_value.md
18+
docs/components/schema/array_prefix_items.md
1719
docs/components/schema/object_dependent_required.md
1820
docs/components/schema/object_dependent_schemas.md
1921
docs/components/schema/object_pattern_properties.md
@@ -46,10 +48,12 @@ src/json_schema_api/components/schema/any_type_dependent_schemas.py
4648
src/json_schema_api/components/schema/any_type_max_contains_value.py
4749
src/json_schema_api/components/schema/any_type_min_contains_value.py
4850
src/json_schema_api/components/schema/any_type_pattern_properties.py
51+
src/json_schema_api/components/schema/any_type_prefix_items.py
4952
src/json_schema_api/components/schema/any_type_property_names.py
5053
src/json_schema_api/components/schema/array_contains_value.py
5154
src/json_schema_api/components/schema/array_max_contains_value.py
5255
src/json_schema_api/components/schema/array_min_contains_value.py
56+
src/json_schema_api/components/schema/array_prefix_items.py
5357
src/json_schema_api/components/schema/object_dependent_required.py
5458
src/json_schema_api/components/schema/object_dependent_schemas.py
5559
src/json_schema_api/components/schema/object_pattern_properties.py

samples/client/3_1_0_json_schema/python/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,12 @@ Class | Description
180180
[AnyTypeMaxContainsValue](docs/components/schema/any_type_max_contains_value.md) |
181181
[AnyTypeMinContainsValue](docs/components/schema/any_type_min_contains_value.md) |
182182
[AnyTypePatternProperties](docs/components/schema/any_type_pattern_properties.md) |
183+
[AnyTypePrefixItems](docs/components/schema/any_type_prefix_items.md) |
183184
[AnyTypePropertyNames](docs/components/schema/any_type_property_names.md) |
184185
[ArrayContainsValue](docs/components/schema/array_contains_value.md) |
185186
[ArrayMaxContainsValue](docs/components/schema/array_max_contains_value.md) |
186187
[ArrayMinContainsValue](docs/components/schema/array_min_contains_value.md) |
188+
[ArrayPrefixItems](docs/components/schema/array_prefix_items.md) |
187189
[ObjectDependentRequired](docs/components/schema/object_dependent_required.md) |
188190
[ObjectDependentSchemas](docs/components/schema/object_dependent_schemas.md) |
189191
[ObjectPatternProperties](docs/components/schema/object_pattern_properties.md) |
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# AnyTypePrefixItems
2+
json_schema_api.components.schema.any_type_prefix_items
3+
```
4+
type: schemas.Schema
5+
```
6+
7+
## validate method
8+
Input Type | Return Type | Notes
9+
------------ | ------------- | -------------
10+
dict, schemas.immutabledict, str, datetime.date, datetime.datetime, uuid.UUID, int, float, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader | schemas.immutabledict, str, float, int, bool, None, tuple, bytes, io.FileIO |
11+
12+
[[Back to top]](#top) [[Back to Component Schemas]](../../../README.md#Component-Schemas) [[Back to README]](../../../README.md)

0 commit comments

Comments
 (0)