Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generators/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|Contains|✗|OAS3
|Default|✗|OAS2,OAS3
|DependentRequired|✗|OAS3
|DependentSchemas|✗|OAS3
|Discriminator|✓|OAS2,OAS3
|Enum|✓|OAS2,OAS3
|ExclusiveMinimum|✓|OAS2,OAS3
Expand Down
1 change: 1 addition & 0 deletions docs/generators/jaxrs-jersey.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|Contains|✗|OAS3
|Default|✗|OAS2,OAS3
|DependentRequired|✗|OAS3
|DependentSchemas|✗|OAS3
|Discriminator|✓|OAS2,OAS3
|Enum|✓|OAS2,OAS3
|ExclusiveMinimum|✓|OAS2,OAS3
Expand Down
1 change: 1 addition & 0 deletions docs/generators/jmeter.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|Contains|✗|OAS3
|Default|✗|OAS2,OAS3
|DependentRequired|✗|OAS3
|DependentSchemas|✗|OAS3
|Discriminator|✓|OAS2,OAS3
|Enum|✓|OAS2,OAS3
|ExclusiveMinimum|✓|OAS2,OAS3
Expand Down
1 change: 1 addition & 0 deletions docs/generators/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|Contains|✗|OAS3
|Default|✗|OAS2,OAS3
|DependentRequired|✗|OAS3
|DependentSchemas|✗|OAS3
|Discriminator|✓|OAS2,OAS3
|Enum|✓|OAS2,OAS3
|ExclusiveMinimum|✓|OAS2,OAS3
Expand Down
1 change: 1 addition & 0 deletions docs/generators/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|Contains|✓|OAS3
|Default|✓|OAS2,OAS3
|DependentRequired|✓|OAS3
|DependentSchemas|✓|OAS3
|Discriminator|✓|OAS2,OAS3
|Enum|✓|OAS2,OAS3
|ExclusiveMinimum|✓|OAS2,OAS3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'const_value_to_name': 'const',
'contains': 'contains',
'dependent_required': 'dependentRequired',
'dependent_schemas': 'dependentSchemas',
'discriminator': 'discriminator',
# default omitted because it has no validation impact
'enum_value_to_name': 'enum',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,28 @@ def validate_dependent_required(
return None


def validate_dependent_schemas(
arg: typing.Any,
dependent_schemas: typing.Mapping[str, typing.Type[SchemaValidator]],
cls: typing.Type,
validation_metadata: ValidationMetadata,
) -> typing.Optional[PathToSchemasType]:
if not isinstance(arg, immutabledict):
return None
path_to_schemas: PathToSchemasType = {}
module_namespace = vars(sys.modules[cls.__module__])
for key, schema in dependent_schemas.items():
if key not in arg:
continue
schema = _get_class(schema, module_namespace)
if validation_metadata.validation_ran_earlier(schema):
add_deeper_validated_schemas(validation_metadata, path_to_schemas)
continue
other_path_to_schemas = schema._validate(arg, validation_metadata=validation_metadata)
update(path_to_schemas, other_path_to_schemas)
return path_to_schemas


validator_type = typing.Callable[[typing.Any, typing.Any, type, ValidationMetadata], typing.Optional[PathToSchemasType]]
json_schema_keyword_to_validator: typing.Mapping[str, validator_type] = {
'types': validate_types,
Expand Down Expand Up @@ -1113,4 +1135,5 @@ def validate_dependent_required(
'max_contains': validate_max_contains,
'const_value_to_name': validate_const,
'dependent_required': validate_dependent_required,
'dependent_schemas': validate_dependent_schemas
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ docs/apis/tags/default_api.md
docs/components/schema/any_type_const_string.md
docs/components/schema/any_type_contains_value.md
docs/components/schema/any_type_dependent_required.md
docs/components/schema/any_type_dependent_schemas.md
docs/components/schema/any_type_max_contains_value.md
docs/components/schema/any_type_min_contains_value.md
docs/components/schema/array_contains_value.md
docs/components/schema/array_max_contains_value.md
docs/components/schema/array_min_contains_value.md
docs/components/schema/object_dependent_required.md
docs/components/schema/object_dependent_schemas.md
docs/components/schema/string_const_string.md
docs/paths/some_path/get.md
docs/paths/some_path/get/responses/response_200/content/application_json/schema.md
Expand All @@ -36,12 +38,14 @@ src/json_schema_api/components/schema/__init__.py
src/json_schema_api/components/schema/any_type_const_string.py
src/json_schema_api/components/schema/any_type_contains_value.py
src/json_schema_api/components/schema/any_type_dependent_required.py
src/json_schema_api/components/schema/any_type_dependent_schemas.py
src/json_schema_api/components/schema/any_type_max_contains_value.py
src/json_schema_api/components/schema/any_type_min_contains_value.py
src/json_schema_api/components/schema/array_contains_value.py
src/json_schema_api/components/schema/array_max_contains_value.py
src/json_schema_api/components/schema/array_min_contains_value.py
src/json_schema_api/components/schema/object_dependent_required.py
src/json_schema_api/components/schema/object_dependent_schemas.py
src/json_schema_api/components/schema/string_const_string.py
src/json_schema_api/components/schemas/__init__.py
src/json_schema_api/configurations/__init__.py
Expand Down
2 changes: 2 additions & 0 deletions samples/client/3_1_0_json_schema/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,14 @@ Class | Description
[AnyTypeConstString](docs/components/schema/any_type_const_string.md) |
[AnyTypeContainsValue](docs/components/schema/any_type_contains_value.md) |
[AnyTypeDependentRequired](docs/components/schema/any_type_dependent_required.md) |
[AnyTypeDependentSchemas](docs/components/schema/any_type_dependent_schemas.md) |
[AnyTypeMaxContainsValue](docs/components/schema/any_type_max_contains_value.md) |
[AnyTypeMinContainsValue](docs/components/schema/any_type_min_contains_value.md) |
[ArrayContainsValue](docs/components/schema/array_contains_value.md) |
[ArrayMaxContainsValue](docs/components/schema/array_max_contains_value.md) |
[ArrayMinContainsValue](docs/components/schema/array_min_contains_value.md) |
[ObjectDependentRequired](docs/components/schema/object_dependent_required.md) |
[ObjectDependentSchemas](docs/components/schema/object_dependent_schemas.md) |
[StringConstString](docs/components/schema/string_const_string.md) |

## Notes for Large OpenAPI documents
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# AnyTypeDependentSchemas
json_schema_api.components.schema.any_type_dependent_schemas
```
type: schemas.Schema
```

## validate method
Input Type | Return Type | Notes
------------ | ------------- | -------------
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 |

[[Back to top]](#top) [[Back to Component Schemas]](../../../README.md#Component-Schemas) [[Back to README]](../../../README.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# ObjectDependentSchemas
json_schema_api.components.schema.object_dependent_schemas
```
type: schemas.Schema
```

## validate method
Input Type | Return Type | Notes
------------ | ------------- | -------------
dict, schemas.immutabledict | schemas.immutabledict |

[[Back to top]](#top) [[Back to Component Schemas]](../../../README.md#Component-Schemas) [[Back to README]](../../../README.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# coding: utf-8

"""
Example
No description provided (generated by Openapi JSON Schema Generator https://github.com/openapi-json-schema-tools/openapi-json-schema-generator) # noqa: E501
The version of the OpenAPI document: 1.0.0
Generated by: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
"""

from __future__ import annotations
from json_schema_api.shared_imports.schema_imports import * # pyright: ignore [reportWildcardImportFromLibrary]



@dataclasses.dataclass(frozen=True)
class A(
schemas.AnyTypeSchema[schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES], typing.Tuple[schemas.OUTPUT_BASE_TYPES, ...]],
):
# any type
min_properties: int = 2

DependentSchemas = typing.TypedDict(
'DependentSchemas',
{
"a": typing.Type[A],
}
)


@dataclasses.dataclass(frozen=True)
class AnyTypeDependentSchemas(
schemas.AnyTypeSchema[schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES], typing.Tuple[schemas.OUTPUT_BASE_TYPES, ...]],
):
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator

Do not edit the class manually.
"""
# any type
dependent_schemas: DependentSchemas = dataclasses.field(default_factory=lambda: schemas.typed_dict_to_instance(DependentSchemas)) # type: ignore

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# coding: utf-8

"""
Example
No description provided (generated by Openapi JSON Schema Generator https://github.com/openapi-json-schema-tools/openapi-json-schema-generator) # noqa: E501
The version of the OpenAPI document: 1.0.0
Generated by: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
"""

from __future__ import annotations
from json_schema_api.shared_imports.schema_imports import * # pyright: ignore [reportWildcardImportFromLibrary]



@dataclasses.dataclass(frozen=True)
class A(
schemas.AnyTypeSchema[schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES], typing.Tuple[schemas.OUTPUT_BASE_TYPES, ...]],
):
# any type
min_properties: int = 2

DependentSchemas = typing.TypedDict(
'DependentSchemas',
{
"a": typing.Type[A],
}
)


@dataclasses.dataclass(frozen=True)
class ObjectDependentSchemas(
schemas.Schema[schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES], tuple]
):
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator

Do not edit the class manually.
"""
types: typing.FrozenSet[typing.Type] = frozenset({schemas.immutabledict})
dependent_schemas: DependentSchemas = dataclasses.field(default_factory=lambda: schemas.typed_dict_to_instance(DependentSchemas)) # type: ignore

@classmethod
def validate(
cls,
arg: typing.Mapping[str, schemas.INPUT_TYPES_ALL],
configuration: typing.Optional[schema_configuration.SchemaConfiguration] = None
) -> schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES]:
return super().validate_base(
arg,
configuration=configuration,
)

Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
from json_schema_api.components.schema.any_type_const_string import AnyTypeConstString
from json_schema_api.components.schema.any_type_contains_value import AnyTypeContainsValue
from json_schema_api.components.schema.any_type_dependent_required import AnyTypeDependentRequired
from json_schema_api.components.schema.any_type_dependent_schemas import AnyTypeDependentSchemas
from json_schema_api.components.schema.any_type_max_contains_value import AnyTypeMaxContainsValue
from json_schema_api.components.schema.any_type_min_contains_value import AnyTypeMinContainsValue
from json_schema_api.components.schema.array_contains_value import ArrayContainsValue
from json_schema_api.components.schema.array_max_contains_value import ArrayMaxContainsValue
from json_schema_api.components.schema.array_min_contains_value import ArrayMinContainsValue
from json_schema_api.components.schema.object_dependent_required import ObjectDependentRequired
from json_schema_api.components.schema.object_dependent_schemas import ObjectDependentSchemas
from json_schema_api.components.schema.string_const_string import StringConstString
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'const_value_to_name': 'const',
'contains': 'contains',
'dependent_required': 'dependentRequired',
'dependent_schemas': 'dependentSchemas',
'discriminator': 'discriminator',
# default omitted because it has no validation impact
'enum_value_to_name': 'enum',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,28 @@ def validate_dependent_required(
return None


def validate_dependent_schemas(
arg: typing.Any,
dependent_schemas: typing.Mapping[str, typing.Type[SchemaValidator]],
cls: typing.Type,
validation_metadata: ValidationMetadata,
) -> typing.Optional[PathToSchemasType]:
if not isinstance(arg, immutabledict):
return None
path_to_schemas: PathToSchemasType = {}
module_namespace = vars(sys.modules[cls.__module__])
for key, schema in dependent_schemas.items():
if key not in arg:
continue
schema = _get_class(schema, module_namespace)
if validation_metadata.validation_ran_earlier(schema):
add_deeper_validated_schemas(validation_metadata, path_to_schemas)
continue
other_path_to_schemas = schema._validate(arg, validation_metadata=validation_metadata)
update(path_to_schemas, other_path_to_schemas)
return path_to_schemas


validator_type = typing.Callable[[typing.Any, typing.Any, type, ValidationMetadata], typing.Optional[PathToSchemasType]]
json_schema_keyword_to_validator: typing.Mapping[str, validator_type] = {
'types': validate_types,
Expand Down Expand Up @@ -1113,4 +1135,5 @@ def validate_dependent_required(
'max_contains': validate_max_contains,
'const_value_to_name': validate_const,
'dependent_required': validate_dependent_required,
'dependent_schemas': validate_dependent_schemas
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# coding: utf-8

"""
Example
No description provided (generated by Openapi JSON Schema Generator https://github.com/openapi-json-schema-tools/openapi-json-schema-generator) # noqa: E501
The version of the OpenAPI document: 1.0.0
Generated by: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
"""

import unittest

import json_schema_api
from json_schema_api.components.schema.any_type_dependent_schemas import AnyTypeDependentSchemas


class TestAnyTypeDependentSchemas(unittest.TestCase):
"""AnyTypeDependentSchemas unit test stubs"""

def test_success_non_ibject(self):
inst = AnyTypeDependentSchemas.validate(True)
assert inst is True

def test_success_non_dependent_schema_key(self):
inst = AnyTypeDependentSchemas.validate({'b': 0})
assert inst == {'b': 0}

def test_success_dependent_schema_key(self):
inst = AnyTypeDependentSchemas.validate({'a': 0, 'b': 1})
assert inst == {'a': 0, 'b': 1}

def test_failure_dependent_schema_key_not_enough_properties(self):
with self.assertRaises(json_schema_api.ApiValueError):
AnyTypeDependentSchemas.validate({'a': 0})


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# coding: utf-8

"""
Example
No description provided (generated by Openapi JSON Schema Generator https://github.com/openapi-json-schema-tools/openapi-json-schema-generator) # noqa: E501
The version of the OpenAPI document: 1.0.0
Generated by: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
"""

import unittest

import json_schema_api
from json_schema_api.components.schema.object_dependent_schemas import ObjectDependentSchemas


class TestObjectDependentSchemas(unittest.TestCase):
"""ObjectDependentSchemas unit test stubs"""

def test_success_non_dependent_schema_key(self):
inst = ObjectDependentSchemas.validate({'b': 0})
assert inst == {'b': 0}

def test_success_dependent_schema_key(self):
inst = ObjectDependentSchemas.validate({'a': 0, 'b': 1})
assert inst == {'a': 0, 'b': 1}

def test_failure_dependent_schema_key_not_enough_properties(self):
with self.assertRaises(json_schema_api.ApiValueError):
ObjectDependentSchemas.validate({'a': 0})


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'const_value_to_name': 'const',
'contains': 'contains',
'dependent_required': 'dependentRequired',
'dependent_schemas': 'dependentSchemas',
'discriminator': 'discriminator',
# default omitted because it has no validation impact
'enum_value_to_name': 'enum',
Expand Down
Loading