0

I use openApi generator to generate apis and models for my react native frontend. I have never had any issues until today, when after generating some (unrelated) apis one of my models started to throw an error every time it has to be used in an api:

Invalid Date
at [native code]:null in toISOString
at app/screens/host/user/EmailVerificationScreen.tsx:110:20 in resend

If i go to the line indicated by the error I find my api parameter:

let parameter = {
        type: VerificationCodeType.Email,
        user: context.state.user, 
    };

The error is thrown by user parameter that has the following structure (generated with openApi):

export interface User {
    /**
     * 
     * @type {number}
     * @memberof User
     */
    id?: number;
    /**
     * 
     * @type {string}
     * @memberof User
     */
    name?: string;
    /**
     * 
     * @type {string}
     * @memberof User
     */
    email?: string;
    /**
     * 
     * @type {string}
     * @memberof User
     */
    phone?: string;
    /**
     * 
     * @type {string}
     * @memberof User
     */
    locale?: string;
    /**
     * 
     * @type {Date}
     * @memberof User
     */
    emailVerifiedAt?: Date;
    /**
     * 
     * @type {Date}
     * @memberof User
     */
    phoneVerifiedAt?: Date;
}

In particular the problem seems to be emailVerifiedAt where, in the generated model, it gets JSONed:

export function UserToJSON(value: User | null): any {
    console.log(value);
    if (value === undefined) {
        return undefined;
    }
    if (value === null) {
        return null;
    }
    return {
        
        'id': value.id,
        'name': value.name,
        'email': value.email,
        'phone': value.phone,
        'locale': value.locale,
        'email_verified_at': value.emailVerifiedAt === undefined ? undefined : (value.emailVerifiedAt.toISOString().substr(0,10)), <= this
        'phone_verified_at': value.phoneVerifiedAt === undefined ? undefined : (value.phoneVerifiedAt.toISOString().substr(0,10)),
    };
}

As you can see it gets checked for undefined (this Screen is only shown when emailVerifiedAt is null (manually swapping the checks from undefined to null doesn't change the result)) and if it's not it gets translated.

As this Screen is shown only when emailVerifiedAt is null that error is kinda blocking.

Before opening an issue on GitHub I wanted to make sure that's not something I did wrong or that I didn't get.

Any idea on what could be the problem?

If you need something else just comment it and I'll add an edit.

Fabio R.
  • 393
  • 3
  • 15

1 Answers1

1

That could be an issue related to your browser. I faced the same error when using Safari, but it works fine in Chrome.

Safari cannot handle dates with - characters, which your API might be returning. If that is the case, then I suggest you check out this solution: https://stackoverflow.com/a/5646753.

Basically you would have to replace all - characters by /. The problem with that is that the date validation occurs in your autogenerated client, which creates your data models before passing them to your application. The returned data models will already include an Invalid Date object.

The OpenAPI generated client would have to be manually edited. You could either provide a custom template with this workaround for your date objects or ask the authors to implement it.

naf
  • 11
  • 1