-
-
Save j5awry/aa8f78791078ddd63a3957ca584e5e60 to your computer and use it in GitHub Desktop.
flask-restplus quick functional site for nested models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright Akamai Technologies, 2019 | |
# For use in testing flask_restplus nested models | |
# Related to issue 275 and others. | |
# Both endpoints should appear | |
# and both models should be, if not the same, then nearly so. | |
from flask import Flask | |
from flask_restplus import Api, Resource, fields | |
from werkzeug.contrib.fixers import ProxyFix | |
app = Flask(__name__) | |
app.wsgi_app = ProxyFix(app.wsgi_app) | |
api = Api(app, version='1.0', title='Testing API', | |
description='Skeleton for Testing Basic Changes', | |
) | |
ns = api.namespace('closet', description='Skeletons live in the closet') | |
# Example model and schema. To use, uncomment. | |
# Or make / import your own TEST_MODEL or TEST_SCHEMA | |
TEST_NEST = api.model("nest", { | |
"key": fields.String(description="Skeleton secrets have keys.") | |
}) | |
TEST_MODEL = { | |
"secret": fields.Nested(TEST_NEST, description="Skeletons have secrets") | |
} | |
# Example test schema! | |
TEST_SCHEMA = { | |
"$schema": "http://json-schema.org/draft-07/schema#", | |
"$id": "testschema", | |
"title": "TEST SCHEMA", | |
"definitions": { | |
"key": { | |
"type": "object", | |
"properties": { | |
"key": { | |
"type": "string" | |
} | |
}, | |
"required": ["key"] | |
} | |
}, | |
"type": "object", | |
"required": [ | |
"secret" | |
], | |
"properties": { | |
"secret": { | |
"type": "array", | |
"items": { | |
"$ref": "#/definitions/key" | |
}, | |
"uniqueItems": True | |
} | |
}, | |
"additionalProperties": False | |
} | |
# Insert your model here! | |
skeleton_model = api.model( | |
"test", | |
TEST_MODEL | |
) | |
# OR json schema! | |
skeleton_jsonschema = api.schema_model("TEST_SCHEMA", TEST_SCHEMA) | |
@ns.route("/model") | |
@api.doc(description="Some skeletons were models") | |
class SkeletonModel(Resource): | |
# pick a style of interaction! | |
# api.expect adds validation, and passes as the model/schema | |
# api.marshall_with will return an OrderedDict, preserving order! | |
# however api.marshall_with does not add a validation step | |
@api.expect(skeleton_model, validate=True) | |
#@api.marshal_with(skeleton_model) | |
def post(self): | |
return api.payload | |
@ns.route("/jsonschema") | |
@api.doc(description="Some skeletons are schema? what?") | |
class SkeletonSchema(Resource): | |
@api.expect(skeleton_jsonschema, validate=True) | |
def post(self): | |
return api.payload | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment