OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.81k stars 6.58k forks source link

[BUG][typescript-fetch] ModelPropertyNaming ignored for list of reference item #5264

Open JulienUsson opened 4 years ago

JulienUsson commented 4 years ago

Bug Report Checklist

Description

ModelPropertyNaming is ignored for list of reference item. Snake case properties are not converted to camel case properties in runtime but the types are correct (in snake case).

openapi-generator version

v4.2.3

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: main
  version: '1.0'
servers:
  - url: 'http://localhost:3000'
paths:
  /foo:
    get:
      summary: Your GET endpoint
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/FooList'
      operationId: get-foo
  '/foo/{foo_id}':
    parameters:
      - schema:
          type: string
        name: foo_id
        in: path
        required: true
    get:
      summary: Your GET endpoint
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Foo'
      operationId: get-foo-foo_id
components:
  schemas:
    Foo:
      title: Foo
      type: object
      properties:
        id:
          type: string
        foo_bar:
          type: string
        bar_foo:
          type: string
        foo:
          type: string
        bar:
          type: string
    FooList:
      title: FooList
      type: array
      items:
        $ref: '#/components/schemas/Foo'
Command line used for generation

docker run --rm -u $(id -u ${USER}):$(id -g ${USER}) -v ${PWD}:/local -w /local openapitools/openapi-generator-cli:v4.2.3 generate --enable-post-process-file --generate-alias-as-model --generator-name typescript-fetch -i ./openapi.yaml -o ./ --model-package types --api-package api -p withSeparateModelsAndApi=true

Actuel output
// models/Foo.ts
/**
 * 
 * @export
 * @interface Foo
 */
export interface Foo {
    /**
     * 
     * @type {string}
     * @memberof Foo
     */
    id?: string;
    /**
     * 
     * @type {string}
     * @memberof Foo
     */
    fooBar?: string;
    /**
     * 
     * @type {string}
     * @memberof Foo
     */
    barFoo?: string;
    /**
     * 
     * @type {string}
     * @memberof Foo
     */
    foo?: string;
    /**
     * 
     * @type {string}
     * @memberof Foo
     */
    bar?: string;
}

export function FooFromJSONTyped(json: any, ignoreDiscriminator: boolean): Foo {
    if ((json === undefined) || (json === null)) {
        return json;
    }
    return {

        'id': !exists(json, 'id') ? undefined : json['id'],
        'fooBar': !exists(json, 'foo_bar') ? undefined : json['foo_bar'],
        'barFoo': !exists(json, 'bar_foo') ? undefined : json['bar_foo'],
        'foo': !exists(json, 'foo') ? undefined : json['foo'],
        'bar': !exists(json, 'bar') ? undefined : json['bar'],
    };
}

// models/FooList.ts
export interface FooList extends Array<Foo> {
}

export function FooListFromJSONTyped(json: any, ignoreDiscriminator: boolean): FooList {
    return json;
}
Expected output
// models/Foo.ts
/**
 * 
 * @export
 * @interface Foo
 */
export interface Foo {
    /**
     * 
     * @type {string}
     * @memberof Foo
     */
    id?: string;
    /**
     * 
     * @type {string}
     * @memberof Foo
     */
    fooBar?: string;
    /**
     * 
     * @type {string}
     * @memberof Foo
     */
    barFoo?: string;
    /**
     * 
     * @type {string}
     * @memberof Foo
     */
    foo?: string;
    /**
     * 
     * @type {string}
     * @memberof Foo
     */
    bar?: string;
}

export function FooFromJSONTyped(json: any, ignoreDiscriminator: boolean): Foo {
    if ((json === undefined) || (json === null)) {
        return json;
    }
    return {

        'id': !exists(json, 'id') ? undefined : json['id'],
        'fooBar': !exists(json, 'foo_bar') ? undefined : json['foo_bar'],
        'barFoo': !exists(json, 'bar_foo') ? undefined : json['bar_foo'],
        'foo': !exists(json, 'foo') ? undefined : json['foo'],
        'bar': !exists(json, 'bar') ? undefined : json['bar'],
    };
}

// models/FooList.ts
export interface FooList extends Array<Foo> {
}

export function FooListFromJSONTyped(json: any, ignoreDiscriminator: boolean): FooList {
    if ((json === undefined) || (json === null)) {
        return json;
    }
    return json.map(item => FooFromJSONTyped(item, ignoreDiscriminator));
}
Related issues/PRs

I found none.

auto-labeler[bot] commented 4 years ago

👍 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.

avendiart commented 4 years ago

Hi, I've encountered the same issue. Patching modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache#L99 resolves the problem, but I'm not 100% confident in the solution. Here is the patch:

    {{#isArrayModel}}
    {{#arrayModelType}}
    // This is a manual change to the template in an attempt to fix:
    // https://github.com/OpenAPITools/openapi-generator/issues/5264
    if ((json === undefined) || (json === null)) {
        return json;
    }
    return (json as Array<any>).map(value => {{arrayModelType}}FromJSON(value, nullToUndefined));
    {{/arrayModelType}}
    {{^arrayModelType}}
    return json;
    {{/arrayModelType}}
    {{/isArrayModel}}
    {{^isArrayModel}}
    return json;
    {{/isArrayModel}}