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

[BUG] [typescript-fetch] (v4.0.0) Generated models no longer produce valid to/from JSON functions - can't compile #2739

Closed
5 of 6 tasks
someone1 opened this issue Apr 25, 2019 · 7 comments

Comments

@someone1
Copy link
Contributor

someone1 commented Apr 25, 2019

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

I've been using a nightly snapshot from early December (shortly after my last PR) to generate my typescript client. I decided to try out the latest beta and am happy with some of the changes/fixes (great job)! However, there seems to have been a change with how models are computed/generated, models that "extend" another model (e.g. allOf with a $ref) no longer seem to be generated as such. Additionally, the change I previously put in to use allVars vs vars seems to be breaking things now whereas before it was necessary.

openapi-generator version

I'm testing via the latest version shipped with the npm package: 4.0.0-beta3
The old version that was working as expected:: 4.0.0-SNAPSHOT (20181206.114535-69)

OpenAPI declaration file content or url
swagger: '2.0'
info:
  title: 'Simple Test'

paths:
  '/get/Simple':
    get:
      summary: Retrieve List of Simple
      operationId: 'get.Simple'
      responses:
        200:
          description: OK
          schema:
            $ref: '#/definitions/ModelFull'

definitions:
  ModelPartial:
    type: object
    required:
      - id
    properties:
      id:
        type: string
      name:
        type: string
  ModelFull:
    allOf:
      - $ref: '#/definitions/ModelPartial'
      - properties:
          creationDate:
            type: string
            format: date-time
            readOnly: true

Generated Code (notice the missing inherited fields including the required field id in ModelFullFromJSON):

/**
 * 
 * @export
 * @interface ModelFull
 */
export interface ModelFull {
    /**
     * 
     * @type {string}
     * @memberof ModelFull
     */
    id: string;
    /**
     * 
     * @type {string}
     * @memberof ModelFull
     */
    name?: string;
    /**
     * 
     * @type {Date}
     * @memberof ModelFull
     */
    readonly creationDate?: Date;
}

export function ModelFullFromJSON(json: any): ModelFull {
    return {
        'creationDate': !exists(json, 'creationDate') ? undefined : new Date(json['creationDate']),
    };
}

Generated code from older generator (notice ModelFull extends ModelPartial and includes all required fields in ModelFullFromJSON):

/**
 * 
 * @export
 * @interface ModelFull
 */
export interface ModelFull extends ModelPartial {
    /**
     * 
     * @type {Date}
     * @memberof ModelFull
     */
    readonly creationDate?: Date;
}

export function ModelFullFromJSON(json: any): ModelFull {
    return {
        'id': json['id'],
        'name': !exists(json, 'name') ? undefined : json['name'],
        'creationDate': !exists(json, 'creationDate') ? undefined : new Date(json['creationDate']),
    };
}

export function ModelFullToJSON(value?: ModelFull): any {
    if (value === undefined) {
        return undefined;
    }
    return {
        'id': value.id,
        'name': value.name,
    };
}

ModelPartial:

/**
 * 
 * @export
 * @interface ModelPartial
 */
export interface ModelPartial {
    /**
     * 
     * @type {string}
     * @memberof ModelPartial
     */
    id: string;
    /**
     * 
     * @type {string}
     * @memberof ModelPartial
     */
    name?: string;
}
Command line used for generation

openapi-generator generate -i test.yaml -g typescript-fetch -o ./test-api

Steps to reproduce

Generate the code from the example yaml and command line I provided.

Related issues/PRs
Suggest a fix

I'm not aware of what happened in the backend for the "parent" attribute to be dropped when generating models and for the allVars not to include everything as it did in the past. If this is truly preferred then I think the allVars can be switched back to vars when generating the *FromJSON functions.

Please let me know if this was intentional and I can try and put in a PR accordingly.

@auto-labeler
Copy link

auto-labeler bot commented Apr 25, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@someone1
Copy link
Contributor Author

I also wanted to mention the issue still exists with the latest snapshot: openapi-generator-cli-4.0.0-20190424.154647-577

@wwang2016
Copy link

Hi
I am having the same issue.
To make it clear, v3.3.4 is good in this regards

Wayne

@someone1
Copy link
Contributor Author

I have a PR that switches back to the #vars variable in the template but I wanted to better understand if the changes that are causing this issue were intentional or not prior to submitting it.

@someone1
Copy link
Contributor Author

someone1 commented May 14, 2019

Just ran a test with the newly released 4.0.0 = still broken but differently.

Now 3 model files are generated: ModelFull.ts, ModelFullAllOf.ts and ModelPartial.ts

ModelFull.ts is even more broken now than before (there are no properties included now!):

/**
 * 
 * @export
 * @interface ModelFull
 */
export interface ModelFull {
    /**
     * 
     * @type {string}
     * @memberof ModelFull
     */
    id: string;
    /**
     * 
     * @type {string}
     * @memberof ModelFull
     */
    name?: string;
    /**
     * 
     * @type {Date}
     * @memberof ModelFull
     */
    readonly creationDate?: Date;
}

export function ModelFullFromJSON(json: any): ModelFull {
    return {
    };
}

export function ModelFullToJSON(value?: ModelFull): any {
    if (value === undefined) {
        return undefined;
    }
    return {
    };
}

ModelFullAllOf.ts looks like ModelFull.ts looked like during beta3.

When trying to compile:

> tsc --outdir ./dist 
models/ModelFull.ts:51:5 - error TS2741: Property 'id' is missing in type '{}' but required in type 'ModelFull'.

CC: @wing328

@someone1 someone1 changed the title [BUG] [typescript-fetch] (v4.0.0-beta3) Generated models no longer extends parent and returns proper objects from JSON [BUG] [typescript-fetch] (v4.0.0) Generated models no longer extends parent and returns proper objects from JSON May 14, 2019
@someone1 someone1 changed the title [BUG] [typescript-fetch] (v4.0.0) Generated models no longer extends parent and returns proper objects from JSON [BUG] [typescript-fetch] (v4.0.0) Generated models no longer produce valid to/from JSON functions - can't compile May 14, 2019
@oytuntez
Copy link

These incompatibilities are becoming more and more serious. We had to switch to v4, but now v4 is causing problems.

Any update here? Or should we fork and apply the patches?

@macjohnny
Copy link
Member

fixed with #2899

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

4 participants