|
1 | 1 | """OpenAPI core schemas factories module"""
|
2 | 2 | import logging
|
3 | 3 |
|
| 4 | +from six import iteritems |
| 5 | + |
4 | 6 | from openapi_core.compat import lru_cache
|
5 | 7 | from openapi_core.schema.properties.generators import PropertiesGenerator
|
6 | 8 | from openapi_core.schema.schemas.models import Schema
|
| 9 | +from openapi_core.schema.schemas.types import Contribution |
7 | 10 |
|
8 | 11 | log = logging.getLogger(__name__)
|
9 | 12 |
|
@@ -86,3 +89,59 @@ def properties_generator(self):
|
86 | 89 |
|
87 | 90 | def _create_items(self, items_spec):
|
88 | 91 | return self.create(items_spec)
|
| 92 | + |
| 93 | + |
| 94 | +class SchemaDictFactory(object): |
| 95 | + |
| 96 | + contributions = ( |
| 97 | + Contribution('type', src_prop_attr='value'), |
| 98 | + Contribution('format'), |
| 99 | + Contribution('properties', is_dict=True, dest_default={}), |
| 100 | + Contribution('required', dest_default=[]), |
| 101 | + Contribution('default'), |
| 102 | + Contribution('nullable', dest_default=False), |
| 103 | + Contribution('all_of', dest_prop_name='allOf', is_list=True, dest_default=[]), |
| 104 | + Contribution('one_of', dest_prop_name='oneOf', is_list=True, dest_default=[]), |
| 105 | + Contribution('additional_properties', dest_prop_name='additionalProperties', dest_default=True), |
| 106 | + Contribution('min_items', dest_prop_name='minItems'), |
| 107 | + Contribution('max_items', dest_prop_name='maxItems'), |
| 108 | + Contribution('min_length', dest_prop_name='minLength'), |
| 109 | + Contribution('max_length', dest_prop_name='maxLength'), |
| 110 | + Contribution('pattern', src_prop_attr='pattern'), |
| 111 | + Contribution('unique_items', dest_prop_name='uniqueItems', dest_default=False), |
| 112 | + Contribution('minimum'), |
| 113 | + Contribution('maximum'), |
| 114 | + Contribution('multiple_of', dest_prop_name='multipleOf'), |
| 115 | + Contribution('exclusive_minimum', dest_prop_name='exclusiveMinimum', dest_default=False), |
| 116 | + Contribution('exclusive_maximum', dest_prop_name='exclusiveMaximum', dest_default=False), |
| 117 | + Contribution('min_properties', dest_prop_name='minProperties'), |
| 118 | + Contribution('max_properties', dest_prop_name='maxProperties'), |
| 119 | + ) |
| 120 | + |
| 121 | + def create(self, schema): |
| 122 | + schema_dict = {} |
| 123 | + for contrib in self.contributions: |
| 124 | + self._contribute(schema, schema_dict, contrib) |
| 125 | + return schema_dict |
| 126 | + |
| 127 | + def _contribute(self, schema, schema_dict, contrib): |
| 128 | + def src_map(x): |
| 129 | + return getattr(x, '__dict__') |
| 130 | + src_val = getattr(schema, contrib.src_prop_name) |
| 131 | + |
| 132 | + if src_val and contrib.src_prop_attr: |
| 133 | + src_val = getattr(src_val, contrib.src_prop_attr) |
| 134 | + |
| 135 | + if contrib.is_list: |
| 136 | + src_val = list(map(src_map, src_val)) |
| 137 | + if contrib.is_dict: |
| 138 | + src_val = dict( |
| 139 | + (k, src_map(v)) |
| 140 | + for k, v in iteritems(src_val) |
| 141 | + ) |
| 142 | + |
| 143 | + if src_val == contrib.dest_default: |
| 144 | + return |
| 145 | + |
| 146 | + dest_prop_name = contrib.dest_prop_name or contrib.src_prop_name |
| 147 | + schema_dict[dest_prop_name] = src_val |
0 commit comments