def test_build_reference(): assert utils.build_reference("schema", 2, "Test") == { "$ref": "#/definitions/Test" } assert utils.build_reference("parameter", 2, "Test") == { "$ref": "#/parameters/Test" } assert utils.build_reference("response", 2, "Test") == { "$ref": "#/responses/Test" } assert utils.build_reference("security_scheme", 2, "Test") == { "$ref": "#/securityDefinitions/Test" } assert utils.build_reference("schema", 3, "Test") == { "$ref": "#/components/schemas/Test" } assert utils.build_reference("parameter", 3, "Test") == { "$ref": "#/components/parameters/Test" } assert utils.build_reference("response", 3, "Test") == { "$ref": "#/components/responses/Test" } assert utils.build_reference("security_scheme", 3, "Test") == { "$ref": "#/components/securitySchemes/Test" }
def get_ref_dict(self, schema): """Method to create a dictionary containing a JSON reference to the schema in the spec """ schema_key = make_schema_key(schema) ref_schema = build_reference("schema", self.openapi_version.major, self.refs[schema_key]) if getattr(schema, "many", False): return {"type": "array", "items": ref_schema} return ref_schema
def resolve_nested_schema(self, schema): """Return the OpenAPI representation of a marshmallow Schema. Adds the schema to the spec if it isn't already present. Typically will return a dictionary with the reference to the schema's path in the spec unless the `schema_name_resolver` returns `None`, in which case the returned dictoinary will contain a JSON Schema Object representation of the schema. :param schema: schema to add to the spec """ try: schema_instance = resolve_schema_instance(schema) # If schema is a string and is not found in registry, # assume it is a schema reference except marshmallow.exceptions.RegistryError: return build_reference("schema", self.openapi_version.major, schema) schema_key = make_schema_key(schema_instance) if schema_key not in self.refs: name = self.schema_name_resolver(schema) if not name: try: json_schema = self.schema2jsonschema(schema_instance) except RuntimeError: raise APISpecError( "Name resolver returned None for schema {schema} which is " "part of a chain of circular referencing schemas. Please" " ensure that the schema_name_resolver passed to" " MarshmallowPlugin returns a string for all circular" " referencing schemas.".format(schema=schema)) if getattr(schema, "many", False): return {"type": "array", "items": json_schema} return json_schema name = get_unique_schema_name(self.spec.components, name) self.spec.components.schema(name, schema=schema) return self.get_ref_dict(schema_instance)
def build_ref(spec, component_type, obj): return build_reference(component_type, spec.openapi_version.major, obj)
def build_ref(component, obj): return build_reference(component, 3, obj)