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
Add enum and type to the JSON schema for single item literals #8944
Add enum and type to the JSON schema for single item literals #8944
Conversation
Not sure what the relnotes tag should be. I did |
CodSpeed Performance ReportMerging #8944 will not alter performanceComparing Summary
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks @dmontagu
After updating to 2.7 we issued a problem with generating frontend api for schema We have some objects, specified by discriminator: class ProfileBase(BaseModel):
guid: str
model_type: str
class ProfileFoo(ProfileBase):
model_type: Literal["FOO"]
# some foo specific fields
class ProfileBar(ProfileBase):
model_type: Literal["BAR"]
# some bar specific fields
Profile = Annotated[
Union[ProfileFoo, ProfileBar],
Field(discriminator="model_type")
]
router = APIRouter(prefix="/profiles")
@router.get("/")
def list_profiles() -> list[Profile]:
return [] On frontend swagger-typescript-api is used for code generation Here is exampe of generated code: /** Model Type */
export enum ProfileFooModelTypeEnum {
FOO = 'FOO',
}
/** ProfileFoo */
export interface ProfileFoo {
/** Guid */
guid: string;
/** Model Type */
model_type: ProfileFooModelTypeEnum;
// For pydantic<2.7 it was like this:
// model_type: 'FOO';
}
// here is generated api request
// discrimitator field is string, but object field is enum
listProfiles: (params: RequestParams = {}) =>
this.request<
(
| ({
model_type: 'FOO';
} & ProfileFoo)
| ({
model_type: 'BAR';
} & ProfileBar)
)[],
AppExceptionSchema
>({
path: `/profiles/`,
method: 'GET',
secure: true,
format: 'json',
...params,
}), For generated api there is a type mismatch, because discriminator field is string and object field is enum. This looks like a bug. Can we remove enum field for Literal with one value, or at least add some flag for this option? |
Hi @fi11er, Thanks for the report - please open another issue with said description and we'll be sure to take a look! |
Closes #8905
However, I think we should be careful about merging this, as any change to the JSON schema usually comes with some complaints. In particular, it's probably a good idea to track down the initial commit adding the use of
constant
and determine if it might be a problem to include the duplicate information about the enum value.Probably also a good idea to make sure this doesn't break swagger UI in some way.
Another option, whether this causes any problems we can find or not, would be to expose the change by way of some config setting which would ensure that people wouldn't be affected unless they opted in, but then they also wouldn't benefit from improvements to integration with some JSON schema tools... 🤷♂️ not sure what's best.