Description
See https://swagger.io/docs/specification/data-models/data-types/#readonly-writeonly
These modifiers are on specific properties and allow for a single definition to be used in both read & write endpoints, but provide a different shape to each -
// ... "Data": { ...
"properties": {
"id": { // Returned by GET, not used in POST/PUT/PATCH
"type": "integer",
"readOnly": true
},
"username": {
"type": "string"
},
"password": { // Used in POST/PUT/PATCH, not returned by GET
"type": "string",
"writeOnly": true
}
}
Currently this just generates a single interface similar to -
export interface components {
schemas: {
Data: {
id: number;
username: string;
password: string;
};
};
}
Unfortunately that makes it unusable for any endpoint as GET doesn't show the password
and POST etc doesn't want an id
. One way to change this is to use multiple interfaces (alternatively using a suffix for the schema might be suitable - but there is a possibility of a name clash that way) - possibly something like -
export interface components {
read_schemas: {
Data: {
id: number;
username: string;
};
};
write_schemas: {
Data: {
username: string;
password: string;
};
};
schemas: {
Data: components["read_schemas"]["Data"] | components["read_schemas"]["Data"];
}
}
Alternatively it could go the other way around, although I feel this makes the typing slightly less useful (adding type guards to code is pretty simple, but they like the union types more) -
export interface components {
read_schemas: {
Data: components["schemas"]["Data"] & {
id: number;
};
};
write_schemas: {
Data: components["schemas"]["Data"] & {
password: string;
};
};
schemas: {
Data: {
username: string;
};
}
}
If there's no readOnly
or writeOnly
properties then nothing would need to change for any schemas that don't use them.