1

We are using a typescript openAPI 3 code generator for our angular project and ran into a problem: The interface is generated correctly for the user login as follows:

export interface TokenObtainPair { 
    email: string;
    password: string;
    readonly access: string;
}

When I now try to write a function based on this to request a new login token I do the following:

login(Email: string, Password: string): Observable<any> {
    const pair: TokenObtainPair = {
      email: Email,
      password: Password,
    }
    return this.apiService.apiTokenCreate({TokenObtainPair: pair });
}

Now angular says that error TS2739: Type '{ email: string; password: string; }' is missing the following properties from type 'TokenObtainPair': access. I could add access: '' to the request and it works, but I would want to do it without that manual intervention. If the field is marked as readonly, it should only be required on read and not on write.

Tillmann
  • 19
  • 1
  • Try adding ? After access: readonly access?: string; If you don’t use the questionmark you are saying that access is mandatory, even when a property is readonly it has to be initialized, only after that you wont be able to change it – Skin_phil Nov 15 '22 at 19:41
  • @Skin_phil Thanks for the quick response, but this is the Swagger generated part. We shouldn't touch this, as far as I am aware. – Tillmann Nov 15 '22 at 20:06
  • Does the `access` field have the [`nullable: true`](https://stackoverflow.com/a/48114322/113116) attribute in your OpenAPI schema? If not, try adding this attribute and see if it changes the generated code to `access?: string`. – Helen Nov 15 '22 at 20:12
  • I understand! Then you have to add access:”” , if you don’t fill a readonly field when you use the constructor, then it is a field you can discard, or better have two different interfaces, one with and one without, so try and rethink the swagger settings – Skin_phil Nov 15 '22 at 20:14
  • Related issue in the OpenAPI Generator repository: [typescript-angular: Generate operation-dependent types according to readOnly=true](https://github.com/OpenAPITools/openapi-generator/issues/13289) – Helen Nov 15 '22 at 20:17
  • @Helen Thanks for your reply, this unfortunately had no effect on the generated code – Tillmann Nov 15 '22 at 21:08

0 Answers0