Example #1
0
    def schema2jsonschema(self, schema):
        if self.openapi_version.major < 3 or not is_value_typed_dict(schema):
            return super().schema2jsonschema(schema)

        schema_type = schema.value_type
        schema_instance = common.resolve_schema_instance(schema_type)
        schema_key = common.make_schema_key(schema_instance)
        if schema_key not in self.refs:
            component_name = self.schema_name_resolver(schema_type)
            self.spec.components.schema(component_name, schema=schema_instance)

        ref_dict = self.get_ref_dict(schema_instance)

        return {
            u'type': u'object',
            u'additionalProperties': ref_dict,
        }
Example #2
0
    def schema2jsonschema(self, schema):
        if self.openapi_version.major < 3 or not is_oneof(schema):
            return super(OneofOpenAPIConverter, self).schema2jsonschema(schema)
        mapping = {}
        oneof = []
        for name, type_schema in schema.type_schemas.items():
            schema_instance = common.resolve_schema_instance(type_schema)
            schema_key = common.make_schema_key(schema_instance)
            if schema_key not in self.refs:
                component_name = self.schema_name_resolver(type_schema) or name
                self.spec.components.schema(component_name, schema=type_schema)
            ref_dict = self.get_ref_dict(schema_instance)
            mapping.update({name: ref_dict['$ref']})
            oneof.append(ref_dict)

        return {
            'oneOf': oneof,
            'discriminator': {
                'propertyName': schema.type_field,
                'mapping': mapping
            }
        }
Example #3
0
    def schema2jsonschema(self, schema):
        if not is_value_typed_dict(schema):
            return super().schema2jsonschema(schema)

        try:
            schema_instance = common.resolve_schema_instance(schema.value_type)
        except ValueError:
            schema_instance = None

        if schema_instance is None:
            properties = field_properties(schema.value_type[0])
        else:
            schema_key = common.make_schema_key(schema_instance)
            if schema_key not in self.refs:
                component_name = self.schema_name_resolver(schema.value_type)
                self.spec.components.schema(component_name,
                                            schema=schema_instance)
            properties = self.get_ref_dict(schema_instance)

        return {
            "type": "object",
            "additionalProperties": properties,
        }
Example #4
0
    def schema2jsonschema(self, schema):
        if not is_value_typed_dict(schema):
            return super().schema2jsonschema(schema)

        if isinstance(schema.value_type, FieldWrapper):
            properties = field_properties(schema.value_type.field)
        elif isinstance(schema.value_type, base.SchemaABC) or (
                isinstance(schema.value_type, type)
                and issubclass(schema.value_type, base.SchemaABC)):
            schema_instance = common.resolve_schema_instance(schema.value_type)
            schema_key = common.make_schema_key(schema_instance)
            if schema_key not in self.refs:
                component_name = self.schema_name_resolver(schema.value_type)
                self.spec.components.schema(component_name,
                                            schema=schema_instance)
            properties = self.get_ref_dict(schema_instance)
        else:
            raise RuntimeError(f"Unsupported value_type: {schema.value_type}")

        return {
            "type": "object",
            "additionalProperties": properties,
        }
Example #5
0
 def test_raise_if_schema_class_passed(self):
     with pytest.raises(TypeError, match="based on a Schema instance"):
         make_schema_key(PetSchema)
Example #6
0
 def test_instances_with_different_modifiers_not_equal(self):
     assert make_schema_key(PetSchema()) != make_schema_key(
         PetSchema(partial=True))
Example #7
0
 def test_different_schemas_not_equal(self):
     assert make_schema_key(PetSchema()) != make_schema_key(SampleSchema())
Example #8
0
 def test_same_schemas_instances_equal(self):
     assert make_schema_key(PetSchema()) == make_schema_key(PetSchema())
 def test_same_schemas_instances_unhashable_modifiers_equal(
         self, structure):
     modifier = [str(i) for i in range(1000)]
     assert make_schema_key(
         PetSchema(load_only=structure(modifier))) == make_schema_key(
             PetSchema(load_only=structure(modifier[::-1])))