-
Notifications
You must be signed in to change notification settings - Fork 27
Add 'mode' field to PydanticDumpOptions TypedDict #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This is used when objects like IPv4Address or other custom objects is not reconised by the json serializer.
|
I'm not sure this is required, the addition of a JSONEncoder here should ensure that it is converted to JSON |
|
I can only have this working when I set mode Without mode from http import HTTPStatus
from ipaddress import IPv4Address
import pydantic
import quart_schema
from quart import Quart
class DemoResp(pydantic.BaseModel):
message: str
ip: IPv4Address
schema = quart_schema.QuartSchema()
app = Quart(__name__)
schema.init_app(app)
@app.get("/hello")
@quart_schema.validate_response(DemoResp, HTTPStatus.OK)
def hello() -> tuple[DemoResp, int]:
return DemoResp(message="Hello, World!", ip=IPv4Address("127.0.0.1")), HTTPStatus.OK
if __name__ == "__main__":
app.run(host="127.0.0.1", port=5000)With mode from http import HTTPStatus
from ipaddress import IPv4Address
import pydantic
import quart_schema
from quart import Quart
class DemoResp(pydantic.BaseModel):
message: str
ip: IPv4Address
schema = quart_schema.QuartSchema()
app = Quart(__name__)
app.config["QUART_SCHEMA_PYDANTIC_DUMP_OPTIONS"] = {"mode": "json"}
schema.init_app(app)
@app.get("/hello")
@quart_schema.validate_response(DemoResp, HTTPStatus.OK)
def hello() -> tuple[DemoResp, int]:
return DemoResp(message="Hello, World!", ip=IPv4Address("127.0.0.1")), HTTPStatus.OK
if __name__ == "__main__":
app.run(host="127.0.0.1", port=5000) |
|
@pgjones from http import HTTPStatus
import pydantic
import quart_schema
from quart import Quart
class StupidApiModel(pydantic.BaseModel):
name: str
version: str
@pydantic.field_validator("version", "name", mode="before")
@classmethod
def _validate_value_dict(cls, v: dict[str, str] | str) -> str:
if isinstance(v, str):
return v
return v["value"]
@pydantic.field_serializer("version", "name", when_used="json")
def _serialize_value_dict(self, v: str) -> dict[str, str]:
return {"value": v}
schema = quart_schema.QuartSchema()
app = Quart(__name__)
schema.init_app(app)
@app.get("/world")
@quart_schema.validate_response(StupidApiModel, HTTPStatus.OK)
def world() -> tuple[StupidApiModel, int]:
return StupidApiModel(name="World", version="1.0"), HTTPStatus.OK
if __name__ == "__main__":
app.run(host="127.0.0.1", port=5000)This incorrectly returns With the json mode from http import HTTPStatus
import pydantic
import quart_schema
from quart import Quart
class StupidApiModel(pydantic.BaseModel):
name: str
version: str
@pydantic.field_validator("version", "name", mode="before")
@classmethod
def _validate_value_dict(cls, v: dict[str, str] | str) -> str:
if isinstance(v, str):
return v
return v["value"]
@pydantic.field_serializer("version", "name", when_used="json")
def _serialize_value_dict(self, v: str) -> dict[str, str]:
return {"value": v}
schema = quart_schema.QuartSchema()
app = Quart(__name__)
app.config["QUART_SCHEMA_PYDANTIC_DUMP_OPTIONS"] = {"mode": "json"}
schema.init_app(app)
@app.get("/world")
@quart_schema.validate_response(StupidApiModel, HTTPStatus.OK)
def world() -> tuple[StupidApiModel, int]:
return StupidApiModel(name="World", version="1.0"), HTTPStatus.OK
if __name__ == "__main__":
app.run(host="127.0.0.1", port=5000)Correctly returns |
|
I'm not sure what is going wrong here, when I try from http import HTTPStatus
from ipaddress import IPv4Address
import pydantic
import quart_schema
from quart import Quart
class DemoResp(pydantic.BaseModel):
message: str
ip: IPv4Address
schema = quart_schema.QuartSchema()
app = Quart(__name__)
schema.init_app(app)
@app.get("/hello")
@quart_schema.validate_response(DemoResp, HTTPStatus.OK)
def hello() -> tuple[DemoResp, int]:
return DemoResp(message="Hello, World!", ip=IPv4Address("127.0.0.1")), HTTPStatus.OK
if __name__ == "__main__":
app.run(host="127.0.0.1", port=5000)it works. Do you have the full stack trace for the |
This is used when objects like IPv4Address or other custom objects is not reconised by the json serializer.
Often you want your model_dump to give you the object not serialized like IPv4Addresses, but you want
model_dump(mode="json")to give you the string.I would even suggest that
mode="json"should be default, but I'm afraid that would break som peoples code.