|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# @Author : llc |
| 3 | +# @Time : 2025/1/6 16:37 |
| 4 | +from typing import Union |
| 5 | + |
| 6 | +import pytest |
| 7 | +from flask import Request |
| 8 | +from pydantic import BaseModel |
| 9 | + |
| 10 | +from flask_openapi3 import OpenAPI |
| 11 | + |
| 12 | +app = OpenAPI(__name__) |
| 13 | +app.config["TESTING"] = True |
| 14 | + |
| 15 | + |
| 16 | +class DogBody(BaseModel): |
| 17 | + a: int = None |
| 18 | + b: str = None |
| 19 | + |
| 20 | + model_config = { |
| 21 | + "openapi_extra": { |
| 22 | + "content_type": "application/vnd.dog+json" |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | +class CatBody(BaseModel): |
| 28 | + c: int = None |
| 29 | + d: str = None |
| 30 | + |
| 31 | + model_config = { |
| 32 | + "openapi_extra": { |
| 33 | + "content_type": "application/vnd.cat+json" |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | +class BsonModel(BaseModel): |
| 39 | + e: int = None |
| 40 | + f: str = None |
| 41 | + |
| 42 | + model_config = { |
| 43 | + "openapi_extra": { |
| 44 | + "content_type": "application/bson" |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | +class ContentTypeModel(BaseModel): |
| 50 | + model_config = { |
| 51 | + "openapi_extra": { |
| 52 | + "content_type": "text/csv" |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | +@app.post("/a", responses={200: Union[DogBody, CatBody, ContentTypeModel, BsonModel]}) |
| 58 | +def index_a(body: Union[DogBody, CatBody, ContentTypeModel, BsonModel]): |
| 59 | + """ |
| 60 | + This may be confusing, if the content-type is application/json, the type of body will be auto parsed to |
| 61 | + DogBody or CatBody, otherwise it cannot be parsed to ContentTypeModel or BsonModel. |
| 62 | + The body is equivalent to the request variable in Flask, and you can use body.data, body.text, etc ... |
| 63 | + """ |
| 64 | + print(body) |
| 65 | + if isinstance(body, Request): |
| 66 | + if body.mimetype == "text/csv": |
| 67 | + # processing csv data |
| 68 | + ... |
| 69 | + elif body.mimetype == "application/bson": |
| 70 | + # processing bson data |
| 71 | + ... |
| 72 | + else: |
| 73 | + # DogBody or CatBody |
| 74 | + ... |
| 75 | + return {"hello": "world"} |
| 76 | + |
| 77 | + |
| 78 | +@app.post("/b", responses={200: Union[ContentTypeModel, BsonModel]}) |
| 79 | +def index_b(body: Union[ContentTypeModel, BsonModel]): |
| 80 | + """ |
| 81 | + This may be confusing, if the content-type is application/json, the type of body will be auto parsed to |
| 82 | + DogBody or CatBody, otherwise it cannot be parsed to ContentTypeModel or BsonModel. |
| 83 | + The body is equivalent to the request variable in Flask, and you can use body.data, body.text, etc ... |
| 84 | + """ |
| 85 | + print(body) |
| 86 | + if isinstance(body, Request): |
| 87 | + if body.mimetype == "text/csv": |
| 88 | + # processing csv data |
| 89 | + ... |
| 90 | + elif body.mimetype == "application/bson": |
| 91 | + # processing bson data |
| 92 | + ... |
| 93 | + else: |
| 94 | + # DogBody or CatBody |
| 95 | + ... |
| 96 | + return {"hello": "world"} |
| 97 | + |
| 98 | + |
| 99 | +@pytest.fixture |
| 100 | +def client(): |
| 101 | + client = app.test_client() |
| 102 | + |
| 103 | + return client |
| 104 | + |
| 105 | + |
| 106 | +def test_openapi(client): |
| 107 | + resp = client.get("/openapi/openapi.json") |
| 108 | + assert resp.status_code == 200 |
| 109 | + |
| 110 | + resp = client.post("/a", json={"a": 1, "b": "2"}) |
| 111 | + assert resp.status_code == 200 |
| 112 | + |
| 113 | + resp = client.post("/a", data="a,b,c\n1,2,3", headers={"Content-Type": "text/csv"}) |
| 114 | + assert resp.status_code == 200 |
0 commit comments