def test_api_register_field_before_and_after_init(self, app, openapi_version): app.config['OPENAPI_VERSION'] = openapi_version api = Api() class CustomField_1(ma.fields.Field): pass class CustomField_2(ma.fields.Field): pass api.register_field(CustomField_1, 'custom string', 'custom') api.init_app(app) api.register_field(CustomField_2, 'custom string', 'custom') class Schema_1(ma.Schema): int_1 = ma.fields.Int() custom_1 = CustomField_1() class Schema_2(ma.Schema): int_2 = ma.fields.Int() custom_2 = CustomField_2() api.spec.components.schema('Schema_1', schema=Schema_1) api.spec.components.schema('Schema_2', schema=Schema_2) schema_defs = get_schemas(api.spec) assert schema_defs['Schema_1']['properties']['custom_1'] == { 'type': 'custom string', 'format': 'custom' } assert schema_defs['Schema_2']['properties']['custom_2'] == { 'type': 'custom string', 'format': 'custom' }
def test_api_register_field_parameters(self, app, mapping): api = Api(app) class CustomField(ma.fields.Field): pass api.register_field(CustomField, *mapping) class Document(ma.Schema): field = CustomField() api.spec.components.schema('Document', schema=Document) if len(mapping) == 2: properties = {'field': {'type': 'custom string'}} # If mapping format is None, it does not appear in the spec if mapping[1] is not None: properties['field']['format'] = mapping[1] else: properties = {'field': {'type': 'integer'}} assert get_schemas(api.spec)['Document'] == { 'properties': properties, 'type': 'object' }
def test_api_register_field_parameters(self, app, mapping): api = Api(app) class CustomField(ma.fields.Field): pass api.register_field(CustomField, *mapping) class Document(ma.Schema): field = CustomField() api.spec.components.schema("Document", schema=Document) if len(mapping) == 2: properties = {"field": {"type": "custom string"}} # If mapping format is None, it does not appear in the spec if mapping[1] is not None: properties["field"]["format"] = mapping[1] else: properties = {"field": {"type": "integer"}} assert get_schemas(api.spec)["Document"] == { "properties": properties, "type": "object", }
def test_api_register_field_before_and_after_init(self, app, openapi_version): app.config["OPENAPI_VERSION"] = openapi_version api = Api() class CustomField_1(ma.fields.Field): pass class CustomField_2(ma.fields.Field): pass api.register_field(CustomField_1, "custom string", "custom") api.init_app(app) api.register_field(CustomField_2, "custom string", "custom") class Schema_1(ma.Schema): int_1 = ma.fields.Int() custom_1 = CustomField_1() class Schema_2(ma.Schema): int_2 = ma.fields.Int() custom_2 = CustomField_2() api.spec.components.schema("Schema_1", schema=Schema_1) api.spec.components.schema("Schema_2", schema=Schema_2) schema_defs = get_schemas(api.spec) assert schema_defs["Schema_1"]["properties"]["custom_1"] == { "type": "custom string", "format": "custom", } assert schema_defs["Schema_2"]["properties"]["custom_2"] == { "type": "custom string", "format": "custom", }
def post(self, key): return {"key": key} @bp.route('/derived') class DerivedDemo(Resource): @bp.arguments(Derived.schema, location="json", required=True, as_kwargs=True) @bp.response(Derived.schema) def post(self, string, integer): return {"integer": integer + 1, "string": string} app = Flask(__name__) app.config['OPENAPI_VERSION'] = '3.0.2' app.config['OPENAPI_URL_PREFIX'] = 'spec' app.config['OPENAPI_SWAGGER_UI_PATH'] = 'swagger' app.config['OPENAPI_SWAGGER_UI_VERSION'] = '3.19.5' app.config['OPENAPI_REDOC_PATH'] = 'redoc' app.config['OPENAPI_REDOC_VERSION'] = 'next' api = Api(app) api.register_field(EnumField, 'string', None) api.register_blueprint(bp)