def parse_enum( self, name: str, obj: JsonSchemaObject, path: List[str], singular_name: bool = False, unique: bool = False, ) -> DataModel: enum_fields: List[DataModelFieldBase] = [] for i, enum_part in enumerate(obj.enum): if obj.type == 'string' or (isinstance(obj.type, list) and 'string' in obj.type): default = f"'{enum_part}'" field_name = enum_part else: default = enum_part if obj.x_enum_varnames: field_name = obj.x_enum_varnames[i] else: field_name = f'{obj.type}_{enum_part}' enum_fields.append( self.data_model_field_type(name=field_name, default=default)) enum_name = self.model_resolver.add( path, name, class_name=True, singular_name=singular_name, singular_name_suffix='Enum', unique=unique, ).name enum = Enum(enum_name, fields=enum_fields) self.append_result(enum) return enum
def create_enum(reference_: Reference) -> DataType: enum = Enum( reference=reference_, fields=enum_fields, path=self.current_source_path, description=obj.description if self.use_schema_description else None, ) self.results.append(enum) return self.data_type(reference=reference_)
def parse_enum( self, name: str, obj: JsonSchemaObject, path: List[str], singular_name: bool = False, unique: bool = True, ) -> DataModel: if not unique: # pragma: no cover warn( f'{self.__class__.__name__}.parse_enum() ignore `unique` argument.' f'An object name must be unique.' f'This argument will be removed in a future version') enum_fields: List[DataModelFieldBase] = [] for i, enum_part in enumerate(obj.enum): if obj.type == 'string' or isinstance(enum_part, str): default = f"'{enum_part}'" field_name = enum_part else: default = enum_part if obj.x_enum_varnames: field_name = obj.x_enum_varnames[i] else: prefix = (obj.type if isinstance(obj.type, str) else type(enum_part).__name__) field_name = f'{prefix}_{enum_part}' enum_fields.append( self.data_model_field_type( name=self.model_resolver.get_valid_name(field_name), default=default, data_type=self.data_type_manager.get_data_type(Types.any), required=True, )) enum_name = self.model_resolver.add( path, name, class_name=True, singular_name=singular_name, singular_name_suffix='Enum', unique=True, loaded=True, ).name enum = Enum( enum_name, fields=enum_fields, path=self.current_source_path, description=obj.description if self.use_schema_description else None, ) self.append_result(enum) return enum
def create_enum(reference_: Reference) -> DataType: enum = Enum( reference=reference_, fields=enum_fields, path=self.current_source_path, description=obj.description if self.use_schema_description else None, custom_template_dir=self.custom_template_dir, type_=_get_type(obj.type, obj.format) if self.use_subclass_enum and isinstance(obj.type, str) else None, ) self.results.append(enum) return self.data_type(reference=reference_)
def parse_enum(self, name: str, obj: JsonSchemaObject) -> DataModel: enum_fields = [] for enum_part in obj.enum: # type: ignore if obj.type == 'string': default = f"'{enum_part}'" field_name = enum_part else: default = enum_part field_name = f'{obj.type}_{enum_part}' enum_fields.append( self.data_model_field_type(name=field_name, default=default)) enum = Enum(self.get_class_name(name), fields=enum_fields) self.append_result(enum) return enum
def parse_enum(self, name: str, obj: JsonSchemaObject) -> DataModel: enum_fields: List[DataModelFieldBase] = [] for i, enum_part in enumerate(obj.enum): if obj.type == 'string' or (isinstance(obj.type, list) and 'string' in obj.type): default = f"'{enum_part}'" field_name = enum_part else: default = enum_part if obj.x_enum_varnames: field_name = obj.x_enum_varnames[i] else: field_name = f'{obj.type}_{enum_part}' enum_fields.append( self.data_model_field_type(name=field_name, default=default)) enum = Enum(self.get_class_name(name), fields=enum_fields) self.append_result(enum) return enum
def parse_enum( self, name: str, obj: JsonSchemaObject, path: List[str], singular_name: bool = False, unique: bool = True, ) -> DataType: if not unique: # pragma: no cover warn( f'{self.__class__.__name__}.parse_enum() ignore `unique` argument.' f'An object name must be unique.' f'This argument will be removed in a future version') enum_fields: List[DataModelFieldBase] = [] if None in obj.enum and obj.type == 'string': # Nullable is valid in only OpenAPI nullable: bool = True enum_times = [e for e in obj.enum if e] else: enum_times = obj.enum nullable = False for i, enum_part in enumerate(enum_times): if obj.type == 'string' or isinstance(enum_part, str): default = f"'{enum_part}'" if obj.x_enum_varnames: field_name = obj.x_enum_varnames[i] else: field_name = enum_part else: default = enum_part if obj.x_enum_varnames: field_name = obj.x_enum_varnames[i] else: prefix = (obj.type if isinstance(obj.type, str) else type(enum_part).__name__) field_name = f'{prefix}_{enum_part}' enum_fields.append( self.data_model_field_type( name=self.model_resolver.get_valid_name(field_name), default=default, data_type=self.data_type_manager.get_data_type(Types.any), required=True, )) if not nullable: reference = self.model_resolver.add( path, name, class_name=True, singular_name=singular_name, singular_name_suffix='Enum', loaded=True, ) enum = Enum( reference=reference, fields=enum_fields, path=self.current_source_path, description=obj.description if self.use_schema_description else None, ) self.append_result(enum) return self.data_type(reference=reference) root_reference = self.model_resolver.add( path, name, class_name=True, singular_name=singular_name, singular_name_suffix='Enum', loaded=True, ) enum_reference = self.model_resolver.add( [*path, 'Enum'], f'{root_reference.name}Enum', class_name=True, singular_name=singular_name, singular_name_suffix='Enum', loaded=True, ) enum = Enum( reference=enum_reference, fields=enum_fields, path=self.current_source_path, description=obj.description if self.use_schema_description else None, ) self.append_result(enum) data_model_root_type = self.data_model_root_type( reference=root_reference, fields=[ self.data_model_field_type( data_type=self.data_type(reference=enum_reference), description=obj.description, example=obj.example, examples=obj.examples, default=obj.default, required=False, nullable=True, ) ], custom_base_class=self.base_class, custom_template_dir=self.custom_template_dir, extra_template_data=self.extra_template_data, path=self.current_source_path, ) self.append_result(data_model_root_type) return self.data_type(reference=root_reference)