OpenAPITools / OpenAPITools/openapi-generator

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

Open
#13,289 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the typescript-angular generator and inspect how schemas are converted into model interfaces and service method parameters. Compare request and response usage for schemas containing required and readOnly properties, then determine how an optional flag and generated request-specific types would affect the existing output and compatibility.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.