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

[REQ] [typescript-angular] Generate operation-dependent types according to readOnly=true #13289

Open
pso-aeb opened this issue Aug 26, 2022 · 2 comments

Comments

@pso-aeb
Copy link

pso-aeb commented Aug 26, 2022

Is your feature request related to a problem? Please describe.

I'm having problems with the generated types against an API that uses readOnly=true, especially in conjunction with required=true.
The API publishes schemas like:

"MyResource": {
  "required": [ "inputField", "outputField" ],
  "type": "object",
  "properties": {
    "inputField": { "type": "string "},
    "outputField": { "type": "string", "readOnly": true }
...

This schema is used as both RequestBody and RespondeBody in operations.
According to OpenAPI spec (AFAIU) readOnly=true means a client does not have to provide a property in a RequestBody, but it will be present in the ResponseBody.

typescript-angular generator produces a type like:

interface MyResource { 
    inputField: string;
    readonly outputField: string;
}

This type is great when a client handles the response from the server.
But if the client has to produce an instance of this type to create a request he has to fill redundant values:

const requestBody: MyResource = {
  inputField: 'A sensible value that the server will receive',
  outputField: 'A redundant value the server will ignore',
}

Describe the solution you'd like

IMO the generator should produce two types:

interface MyResourceRequest { 
    inputField: string;
}
interface MyResource { 
    inputField: string;
    readonly outputField: string;
}

MyResourceRequest should be used as parameter in the corresponding service implementations instead of MyResource.

...of course this would be a breaking change. So maybe it would need a dedicated flag to activate this.

Describe alternatives you've considered

ATM I am circumventing the typescript compiler with hacky casts to get it done.

@sevenclev
Copy link

#4190

@StefanKern
Copy link

Hello, is there any timeline / plan to support this?

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

No branches or pull requests

3 participants