OpenAPITools / OpenAPITools/openapi-generator

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

Open
#5,264 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

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.

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

Reproduce the issue with the provided openapi.yaml and the typescript-fetch generation command, then inspect the generated models/FooList.ts and models/Foo.ts files. Trace the typescript-fetch generator entry point that handles array items and compare the generated deserialization with the expected FooListFromJSONTyped behavior; done means the generated list correctly processes referenced Foo items and regression coverage verifies it.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.