def test_resolve_schema_dict_auto_reference(self, schema): def resolver(schema): return schema.__name__ spec = APISpec( title="Test auto-reference", version="0.1", openapi_version="2.0", plugins=(SerpycoPlugin( schema_name_resolver=schema_name_resolver), ), ) assert {} == get_definitions(spec) spec.components.schema("analysis", schema=schema) spec.path( "/test", operations={ "get": { "responses": { "200": { "schema": { "$ref": "#/definitions/analysis" } } } } }, ) definitions = get_definitions(spec) assert 3 == len(definitions) assert "analysis" in definitions assert "SampleSchema" in definitions assert "RunSchema_exclude_sample" in definitions
def make_spec(openapi_version): s_plugin = SerpycoPlugin() spec = APISpec( title="Validation", version="0.1", openapi_version=openapi_version, plugins=(s_plugin, ), ) return namedtuple("Spec", ("spec", "serpyco_plugin", "openapi"))(spec, s_plugin, s_plugin.openapi)
def generate_schema_ref(self, main_plugin: SerpycoPlugin) -> dict: """ Return OpenApi $ref in a dict, eg. {"$ref": "#/definitions/MySchema"} """ schema_usage = self.schema_class_resolver(main_plugin) ref = { "$ref": "#/definitions/{}".format( main_plugin.schema_name_resolver( schema_usage.schema, **schema_usage.plugin_name_resolver_kwargs)) } return ref
def create_apispec_plugin( cls, schema_name_resolver: typing.Optional[typing.Callable] = None ) -> BasePlugin: schema_name_resolver = schema_name_resolver or schema_name_resolver_ return SerpycoPlugin(schema_name_resolver=schema_name_resolver)