|
| 1 | +from pydantic import BaseModel |
| 2 | + |
| 3 | +from lilya import __version__ |
| 4 | +from lilya.contrib.openapi.datastructures import OpenAPIResponse |
| 5 | +from lilya.contrib.openapi.decorator import openapi |
| 6 | +from lilya.routing import Path |
| 7 | +from lilya.testclient import create_client |
| 8 | + |
| 9 | + |
| 10 | +class ErrorResponse(BaseModel): |
| 11 | + detail: str |
| 12 | + message: str |
| 13 | + |
| 14 | + |
| 15 | +class User(BaseModel): |
| 16 | + name: str |
| 17 | + age: int |
| 18 | + |
| 19 | + |
| 20 | +@openapi( |
| 21 | + summary="A test", |
| 22 | + description="A test", |
| 23 | + responses={ |
| 24 | + 400: OpenAPIResponse(model=ErrorResponse, description="Bad Request"), |
| 25 | + 201: OpenAPIResponse(model=User, description="Ok"), |
| 26 | + }, |
| 27 | +) |
| 28 | +async def create(user: User): |
| 29 | + return user |
| 30 | + |
| 31 | + |
| 32 | +def test_responses_decorator(test_client_factory): |
| 33 | + with create_client( |
| 34 | + routes=[ |
| 35 | + Path("/create", handler=create, methods=["POST"]), |
| 36 | + ] |
| 37 | + ) as client: |
| 38 | + response = client.get("/openapi.json") |
| 39 | + |
| 40 | + assert response.status_code == 200 |
| 41 | + |
| 42 | + assert response.json() == { |
| 43 | + "openapi": "3.1.0", |
| 44 | + "info": { |
| 45 | + "title": "Lilya", |
| 46 | + "version": __version__, |
| 47 | + "summary": "Lilya application", |
| 48 | + "description": "Yet another framework/toolkit that delivers.", |
| 49 | + "contact": { |
| 50 | + "name": "Lilya", |
| 51 | + "url": "https://lilya.dev", |
| 52 | + "email": "admin@myapp.com", |
| 53 | + }, |
| 54 | + }, |
| 55 | + "paths": { |
| 56 | + "/create": { |
| 57 | + "post": { |
| 58 | + "operationId": None, |
| 59 | + "summary": "A test", |
| 60 | + "description": "A test", |
| 61 | + "requestBody": { |
| 62 | + "content": { |
| 63 | + "application/json": { |
| 64 | + "schema": { |
| 65 | + "properties": { |
| 66 | + "name": {"title": "Name", "type": "string"}, |
| 67 | + "age": {"title": "Age", "type": "integer"}, |
| 68 | + }, |
| 69 | + "required": ["name", "age"], |
| 70 | + "title": "User", |
| 71 | + "type": "object", |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + }, |
| 76 | + "responses": { |
| 77 | + "400": { |
| 78 | + "description": "Bad Request", |
| 79 | + "content": { |
| 80 | + "application/json": { |
| 81 | + "schema": {"$ref": "#/components/schemas/ErrorResponse"} |
| 82 | + } |
| 83 | + }, |
| 84 | + }, |
| 85 | + "201": { |
| 86 | + "description": "Ok", |
| 87 | + "content": { |
| 88 | + "application/json": { |
| 89 | + "schema": {"$ref": "#/components/schemas/User"} |
| 90 | + } |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + } |
| 95 | + } |
| 96 | + }, |
| 97 | + "components": { |
| 98 | + "schemas": { |
| 99 | + "ErrorResponse": { |
| 100 | + "properties": { |
| 101 | + "detail": {"title": "Detail", "type": "string"}, |
| 102 | + "message": {"title": "Message", "type": "string"}, |
| 103 | + }, |
| 104 | + "required": ["detail", "message"], |
| 105 | + "title": "ErrorResponse", |
| 106 | + "type": "object", |
| 107 | + }, |
| 108 | + "User": { |
| 109 | + "properties": { |
| 110 | + "name": {"title": "Name", "type": "string"}, |
| 111 | + "age": {"title": "Age", "type": "integer"}, |
| 112 | + }, |
| 113 | + "required": ["name", "age"], |
| 114 | + "title": "User", |
| 115 | + "type": "object", |
| 116 | + }, |
| 117 | + } |
| 118 | + }, |
| 119 | + "servers": [{"url": "/"}], |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | +@openapi() |
| 124 | +async def create_again(user: User): |
| 125 | + return user |
| 126 | + |
| 127 | + |
| 128 | +def test_responses_decorator_simple(test_client_factory): |
| 129 | + with create_client( |
| 130 | + routes=[ |
| 131 | + Path("/item", handler=create_again, methods=["POST"]), |
| 132 | + ] |
| 133 | + ) as client: |
| 134 | + response = client.get("/openapi.json") |
| 135 | + |
| 136 | + assert response.status_code == 200 |
| 137 | + |
| 138 | + assert response.json() == { |
| 139 | + "openapi": "3.1.0", |
| 140 | + "info": { |
| 141 | + "title": "Lilya", |
| 142 | + "version": __version__, |
| 143 | + "summary": "Lilya application", |
| 144 | + "description": "Yet another framework/toolkit that delivers.", |
| 145 | + "contact": { |
| 146 | + "name": "Lilya", |
| 147 | + "url": "https://lilya.dev", |
| 148 | + "email": "admin@myapp.com", |
| 149 | + }, |
| 150 | + }, |
| 151 | + "paths": { |
| 152 | + "/item": { |
| 153 | + "post": { |
| 154 | + "operationId": None, |
| 155 | + "requestBody": {"content": {"application/json": {"schema": {}}}}, |
| 156 | + "responses": {"200": {"description": "Successful response"}}, |
| 157 | + } |
| 158 | + } |
| 159 | + }, |
| 160 | + "components": {"schemas": {}}, |
| 161 | + "servers": [{"url": "/"}], |
| 162 | + } |
0 commit comments