diff --git a/README.md b/README.md index c784093..709e27c 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,19 @@ flask-dantic-schema name: str @app.post("/") - @validate(body=Todo, responses={200: TodoResponse}) + @validate(body=Todo, responses=TodoResponse) def create_todo(): ... # Do something with data, e.g. save to the DB - return data + return dict(id=1, name="2") + + @app.put("/") + @validate( + body=Todo, + responses={200: TodoResponse, 400: TodoResponse} + ) + def update_todo(): + ... # Do something with data, e.g. save to the DB + return TodoResponse(id=1, name="123") app.cli.add_command(generate_schema_command) diff --git a/schema_validator/validation.py b/schema_validator/validation.py index a5dce52..cde6a9b 100644 --- a/schema_validator/validation.py +++ b/schema_validator/validation.py @@ -1,3 +1,5 @@ +import json + from dataclasses import asdict, is_dataclass from enum import Enum, auto from functools import wraps @@ -76,6 +78,9 @@ def check_response(result, response_model: Dict[int, PydanticModel]): else: value = result + if isinstance(value, Response): + value = value.get_json() + status = 200 if status_or_headers is not None and not isinstance( status_or_headers, (Headers, dict, list) diff --git a/tests/test_validation.py b/tests/test_validation.py index ffaf3a5..26df7ac 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -4,7 +4,7 @@ import pytest from pydantic import BaseModel from pydantic.dataclasses import dataclass as pydantic_dataclass -from flask import Flask +from flask import Flask, jsonify from schema_validator import ( DataSource, @@ -137,7 +137,7 @@ def test_response_validation(model: Any, return_value: Any, @app.route("/") @validate(responses=model) def item() -> ResponseReturnValue: - return return_value + return jsonify(return_value) test_client = app.test_client() response = test_client.get("/")