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

from_pydantic with id support #243

Open
alexander793 opened this issue Oct 26, 2023 · 1 comment
Open

from_pydantic with id support #243

alexander793 opened this issue Oct 26, 2023 · 1 comment

Comments

@alexander793
Copy link

What do I want to achive
I want kor to generate a Typescript description like the following:

bookings: Array<{
 project: string
 date: string
 amount: {
  value: number
  unit: string
 }
}>

What did I try
Define the following pydantic schema:

class Amount(BaseModel):
    value: int
    unit: str

class Booking(BaseModel):
    project: str
    date: str
    amount: Amount

class Bookings(BaseModel):
    bookings: List[Booking]

Bookings(bookings=...).json() generates the desired output {"bookings": [{"project": "...", "date": "...", ' '"amount": {"value": "...", "unit": "..."}}]}, so I think the schema definition is correct.
The output of the current from_pydantic implementation is the following:
Call:

booking_schema, validator = from_pydantic(
      Bookings,
      description="",
      examples=booking_examples,
      many=False,
  )

Output:

 bookings: {
  bookings: Array<{
   project: string
   date: string
   amount: {
    value: number
    unit: string
   }
  }>
 }

To overcome this, I tried:
Call:

booking_schema, validator = from_pydantic(
      Booking,
      description="",
      examples=booking_examples,
      many=True,
  )

Output:

 booking: Array<{
   project: string
   date: string
   amount: {
    value: number
    unit: string
   }
  }>

The output is better, but I need the string "bookings" (with an "s"), and couldn't find a way to accomplish this.

How I think it may be solved
Add parameter id to from_pydantic method.
Currently the from_pydantic method allows for the definition of a description, examples and the many flag.
In my opinion the id parameter is missing. Since it is possible to define it in a kor-Object, it should also be possible to provide it to this method. This way it will be possible to set the name of the root object, in my case to "bookings".

@alexander793
Copy link
Author

alexander793 commented Oct 26, 2023

If you think this issue describes a valid point, I'll be glad to submit a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant