11"""OpenAPI core schemas factories module"""
22import logging
33
4+ from six import iteritems
5+
46from openapi_core .compat import lru_cache
57from openapi_core .schema .properties .generators import PropertiesGenerator
68from openapi_core .schema .schemas .models import Schema
9+ from openapi_core .schema .schemas .types import Contribution
710
811log = logging .getLogger (__name__ )
912
@@ -50,11 +53,11 @@ def create(self, schema_spec):
5053
5154 all_of = []
5255 if all_of_spec :
53- all_of = map (self .create , all_of_spec )
56+ all_of = list ( map (self .create , all_of_spec ) )
5457
5558 one_of = []
5659 if one_of_spec :
57- one_of = map (self .create , one_of_spec )
60+ one_of = list ( map (self .create , one_of_spec ) )
5861
5962 items = None
6063 if items_spec :
@@ -76,6 +79,7 @@ def create(self, schema_spec):
7679 exclusive_maximum = exclusive_maximum ,
7780 exclusive_minimum = exclusive_minimum ,
7881 min_properties = min_properties , max_properties = max_properties ,
82+ _source = schema_deref ,
7983 )
8084
8185 @property
@@ -85,3 +89,59 @@ def properties_generator(self):
8589
8690 def _create_items (self , items_spec ):
8791 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