This class is a part of a project:
from pydantic import BaseModel, StrictInt, constr, validator
class Person(BaseModel):
person_id: StrictInt
person_name: constr(strip_whitespace=True, strict=True)
@validator('person_id')
def person_id_validator(cls, val):
assert val is not None
return val
@validator('person_name')
def person_name_validator(cls, val):
assert val != ''
return val
When the API-spec is generated for this class using FastAPI().openapi(), the type of the variable person_id is always integer and consequently, the Java client generated from that spec always has the wrapper class Integer for the variable personId. However, personId can be a very long number (>5,000,000,000) which I can't keep in an Integer.
Pydantic itself doesn't have a Long built-in. Python handles int32 and int64 internally so I cannot control that either. I tried using numpy.int64 and torch.long with the help of arbitrary_types_allowed in class Config but that always fails at runtime with the message
ValueError: Value not declarable with JSON Schema, field: name='person_id' type=int64 required=True.
How do I achieve this?
In OpenAPI, long integers are defined as:
type: integer
format: int64
The key is to use the proper format. When using FastAPI/Pydantic, you can add the format attribute as follows:
person_id: StrictInt = Field(..., format='int64')