fern
fern copied to clipboard
[Feature] Exclude readOnly fields for POST / PUT requests from models
Problem description
It's possible to mark a field as readOnly in openapi: https://swagger.io/docs/specification/data-models/data-types/
You can use the readOnly and writeOnly keywords to mark specific properties as read-only or write-only. This is useful, for example, when GET returns more properties than used in POST – you can use the same schema in both GET and POST and mark the extra properties as readOnly. readOnly properties are included in responses but not in requests, and writeOnly properties may be sent in requests but not in responses.
When we generate our sdk (python) these fields will be available both for GET and POST requests, because they share the same model. This is very confusing for the user, since they will not know which of the fields they actually have to set for the POST request.
Example:
type: object
properties:
id:
type: string
readOnly: true
name:
type: string
Would generated something like:
class TheObject:
id: string
name: string
def get_object() -> TheObject:
....
def create_object(obj: TheObject):
....
client.create_object(obj=TheObject(name="new-name")) # how do I know which fields to set here?
Why would it be useful?
I will quote the openapi documentation:
You can use the readOnly and writeOnly keywords to mark specific properties as read-only or write-only. This is useful, for example, when GET returns more properties than used in POST – you can use the same schema in both GET and POST and mark the extra properties as readOnly.
Describe the solution (optional)
Ideally the POST request would accept a model where only writable fields are available.
Hi there @raphaelcristal, we've pushed a fix for this, do you mind trying the latest python generator to ensure it's working for you as expected? Version 0.11.10
Hi there @raphaelcristal, we've pushed a fix for this, do you mind trying the latest python generator to ensure it's working for you as expected? Version 0.11.10
Sorry I haven't looked at this in a while. What exactly was the fix? What has changed?
I'm also interested in this, currently readOnly fields are appearing in the request body for the generated docs where I'd expect it to only appear in the response.