Skip to content
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

Merged

Conversation

dmontagu
Copy link
Contributor

@dmontagu dmontagu commented Mar 4, 2024

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.

@dmontagu dmontagu added the relnotes-change Used for changes to existing functionality which don't have a better categorization. label Mar 4, 2024
@dmontagu
Copy link
Contributor Author

dmontagu commented Mar 4, 2024

Not sure what the relnotes tag should be. I did relnotes-change since it's arguably not a bugfix but not sure what's best.

Copy link

codspeed-hq bot commented Mar 4, 2024

CodSpeed Performance Report

Merging #8944 will not alter performance

Comparing dmontagu/add-enum-to-json-schema-single-item-literal (02ab56d) with main (af41c8e)

Summary

✅ 10 untouched benchmarks

Copy link
Member

@sydney-runkle sydney-runkle left a 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

@sydney-runkle sydney-runkle mentioned this pull request Mar 12, 2024
16 tasks
@sydney-runkle sydney-runkle merged commit 18d39fe into main Mar 12, 2024
54 of 55 checks passed
@sydney-runkle sydney-runkle deleted the dmontagu/add-enum-to-json-schema-single-item-literal branch March 12, 2024 15:03
@fi11er
Copy link

fi11er commented Apr 15, 2024

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
https://github.com/acacode/swagger-typescript-api

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.
image

This looks like a bug. Can we remove enum field for Literal with one value, or at least add some flag for this option?

@sydney-runkle
Copy link
Member

Hi @fi11er,

Thanks for the report - please open another issue with said description and we'll be sure to take a look!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes-change Used for changes to existing functionality which don't have a better categorization.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Literal annotation does not render type to json-schema
3 participants