1- from copy import deepcopy
21from pathlib import Path
32from typing import Any
43
76from marshmallow import RAISE
87from marshmallow import Schema
98from marshmallow import validate
9+ from marshmallow .fields import Field
1010
11- from ..openapi import OpenAPI
11+ from schema_first .openapi import OpenAPI
12+ from schema_first .specification .spilli_api import ConverterOpenAPIToSpilliAPI
1213
1314FIELDS_VIA_TYPES = {
1415 'boolean' : fields .Boolean ,
3435class Specification :
3536 def __init__ (self , spec_file : Path | str ):
3637 self .openapi = OpenAPI (spec_file )
38+ self .spilli_api = None
3739 self .reassembly_spec = None
3840
3941 @staticmethod
@@ -132,13 +134,17 @@ def _convert_object_field(self, open_api_schema: dict) -> type[Schema]:
132134
133135 return Schema .from_dict (marshmallow_schema )
134136
135- def _convert_array_field (self , open_api_schema : dict ) -> type [ Schema ] :
137+ def _convert_array_field (self , open_api_schema : dict ) -> Field :
136138 array_item_schema = open_api_schema ['items' ]
137- array_schema = self ._convert_object_field (array_item_schema )
138- array_schema .opts .many = True
139- return array_schema
139+ if array_item_schema ['type' ] == 'object' :
140+ nested_field = self ._convert_object_field (array_item_schema )
141+ array_field = fields .Nested (nested_field , many = True )
142+ else :
143+ nested_field = FIELDS_VIA_TYPES [array_item_schema ['type' ]]()
144+ array_field = fields .List (nested_field )
145+ return array_field
140146
141- def _reassembly_of_schemas (self , obj : Any ) -> Any :
147+ def _reassembly_of_schemas (self , obj : Any ) -> None :
142148 if isinstance (obj , dict ):
143149 for k , v in obj .items ():
144150 # Checking for object type is needed to skip already resolved schemes.
@@ -148,10 +154,10 @@ def _reassembly_of_schemas(self, obj: Any) -> Any:
148154 else :
149155 self ._reassembly_of_schemas (v )
150156
151- def load (self ) -> ' Specification' :
157+ def load (self ) -> Specification :
152158 self .openapi .load ()
153- self .reassembly_spec = deepcopy (self .openapi .raw_spec )
154159
160+ self .reassembly_spec = ConverterOpenAPIToSpilliAPI (self .openapi .raw_spec ).convert ()
155161 self ._reassembly_of_schemas (self .reassembly_spec )
156162
157163 return self
0 commit comments