openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[typescript-fetch]Improved error when specifying primitive type with oneOf

Open akira-furukawa-stb opened this issue 1 year ago • 1 comments

It's part of this issue.(https://github.com/OpenAPITools/openapi-generator/issues/12256)

Change details

  • Added the ability to skip importing primitive types.
  • Created private functions for primitive types according to existing naming.
openapi: 3.0.1
info:
  version: 1.0.0
  title: Example - Handling Multiple Data Types with oneOf
  license:
    name: MIT
servers:
  - url: http://api.example.com/v1
paths:
  /example-primitive:
    get:
      tags:
        - example
      operationId: getExamplePrimitive
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PrimitiveTypes'
components:
  schemas:
    PrimitiveTypes:
      oneOf:
        - type: string
        - type: number
        - type: integer
          format: int64
        - type: boolean
/* tslint:disable */
/* eslint-disable */
/**
 * Example - Handling Multiple Data Types with oneOf
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 1.0.0
 * 
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */


/**
 * @type PrimitiveTypes
 * 
 * @export
 */
export type PrimitiveTypes = boolean | number | string;

export function PrimitiveTypesFromJSON(json: any): PrimitiveTypes {
    return PrimitiveTypesFromJSONTyped(json, false);
}

export function PrimitiveTypesFromJSONTyped(json: any, ignoreDiscriminator: boolean): PrimitiveTypes {
    if (json == null) {
        return json;
    }
    if (instanceOfboolean(json)) {
        return booleanFromJSONTyped(json, true);
    }
    if (instanceOfnumber(json)) {
        return numberFromJSONTyped(json, true);
    }
    if (instanceOfstring(json)) {
        return stringFromJSONTyped(json, true);
    }
}

export function PrimitiveTypesToJSON(value?: PrimitiveTypes | null): any {
    if (value == null) {
        return value;
    }

    if (instanceOfboolean(value)) {
        return booleanToJSON(value as boolean);
    }
    if (instanceOfnumber(value)) {
        return numberToJSON(value as number);
    }
    if (instanceOfstring(value)) {
        return stringToJSON(value as string);
    }

    return {};
}

function booleanFromJSONTyped(json: any, ignoreDiscriminator: boolean): boolean | null {
    return typeof json === 'boolean' ? json : null;
}

function instanceOfboolean(value: any): boolean {
    return typeof value === 'boolean';
}

function booleanToJSON(value: boolean): any {
    return value;
}

function numberFromJSONTyped(json: any, ignoreDiscriminator: boolean): number | null {
    return typeof json === 'number' ? json : null;
}

function instanceOfnumber(value: any): boolean {
    return typeof value === 'number';
}

function numberToJSON(value: number): any {
    return value;
}

function stringFromJSONTyped(json: any, ignoreDiscriminator: boolean): string | null {
    return typeof json === 'string' ? json : null;
}

function instanceOfstring(value: any): boolean {
    return typeof value === 'string';
}

function stringToJSON(value: string): any {
    return value;
}

export function instanceOfPrimitiveTypes(value: any): boolean {
    return instanceOfboolean(value) || instanceOfnumber(value) || instanceOfstring(value);
}

  • Also supports arrays.
openapi: 3.0.1
info:
  version: 1.0.0
  title: Example - Handling Multiple Data Types with oneOf
  license:
    name: MIT
servers:
  - url: http://api.example.com/v1
paths:
  /example-array:
    get:
      tags:
        - example-array:
      operationId: getExampleArray
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ArrayTypes'

components:
  schemas:
    ArrayTypes:
      oneOf:
        - type: array
          items:
            type: string
        - type: array
          items:
            type: number
        - type: array
          items:
            type: integer
        - type: array
          items:
            type: boolean
/* tslint:disable */
/* eslint-disable */
/**
 * Example - Handling Multiple Data Types with oneOf
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 1.0.0
 * 
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */


/**
 * @type ArrayTypes
 * 
 * @export
 */
export type ArrayTypes = Array<boolean> | Array<number> | Array<string>;

export function ArrayTypesFromJSON(json: any): ArrayTypes {
    return ArrayTypesFromJSONTyped(json, false);
}

export function ArrayTypesFromJSONTyped(json: any, ignoreDiscriminator: boolean): ArrayTypes {
    if (json == null) {
        return json;
    }
    if (instanceOfbooleanArray(json)) {
        return booleanArrayFromJSONTyped(json, true);
    }
    if (instanceOfnumberArray(json)) {
        return numberArrayFromJSONTyped(json, true);
    }
    if (instanceOfstringArray(json)) {
        return stringArrayFromJSONTyped(json, true);
    }
}

export function ArrayTypesToJSON(value?: ArrayTypes | null): any {
    if (value == null) {
        return value;
    }

    if (instanceOfbooleanArray(value)) {
        return booleanArrayToJSON(value as Array<boolean>);
    }
    if (instanceOfnumberArray(value)) {
        return numberArrayToJSON(value as Array<number>);
    }
    if (instanceOfstringArray(value)) {
        return stringArrayToJSON(value as Array<string>);
    }

    return {};
}


function instanceOfbooleanArray(value: any): boolean {
    return Array.isArray(value) && value.every(element => typeof element === 'boolean');
}

function booleanArrayFromJSONTyped(json: any, ignoreDiscriminator: boolean): Array<boolean> | null {
    if (!instanceOfbooleanArray(json)) {
        return null;
    }
    return json;
}

function booleanArrayToJSON(value: Array<boolean>): any {
    return value;
}

function instanceOfnumberArray(value: any): boolean {
    return Array.isArray(value) && value.every(element => typeof element === 'number');
}

function numberArrayFromJSONTyped(json: any, ignoreDiscriminator: boolean): Array<number> | null {
    if (!instanceOfnumberArray(json)) {
        return null;
    }
    return json;
}

function numberArrayToJSON(value: Array<number>): any {
    return value;
}

function instanceOfstringArray(value: any): boolean {
    return Array.isArray(value) && value.every(element => typeof element === 'string');
}

function stringArrayFromJSONTyped(json: any, ignoreDiscriminator: boolean): Array<string> | null {
    if (!instanceOfstringArray(json)) {
        return null;
    }
    return json;
}

function stringArrayToJSON(value: Array<string>): any {
    return value;
}

  • If you specify a schema that is not a primitive type, it will work as before.

PR checklist

  • [x] Read the contribution guidelines.
  • [x] Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • [x] Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH) Commit all changed files. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • [x] File the PR against the correct branch: master (upcoming 7.6.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • [x] If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

akira-furukawa-stb avatar May 23 '24 06:05 akira-furukawa-stb

@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02) @davidgamero (2022/03) @mkusaka (2022/04)

Dear technical committee members of TypeScript, please check this pull request if you have time. 🙇

akira-furukawa-stb avatar May 23 '24 06:05 akira-furukawa-stb

@macjohnny sorry to ping you but you appear to have been active on similar issues/PRs. Is there any impediment to getting this merged?

Thanks

hrobertson avatar Jan 18 '25 14:01 hrobertson

@hrobertson sorry, this was overlooked, but also its non-trivial to review. it would make it easier and quicker to review if

  • we could split the PR into several parts (if possible)
  • we would have an example input and output in the PR description that is trimmed down to the relevant parts, so its easy to understand what changes

sidenote: i am not familiar with the mustache lambdas, and i wonder whether we can find a simpler approach by correctly processing the imports in the java code, as opposed to post-processing the generated code with string manipulation.

macjohnny avatar Jan 21 '25 13:01 macjohnny

@akira-furukawa-stb Are you still interested in this MR?

See @macjohnny 's comment https://github.com/OpenAPITools/openapi-generator/pull/18740#issuecomment-2604705453

hrobertson avatar Jan 21 '25 17:01 hrobertson

@hrobertson Thanks. It will take time to incorporate the points raised due to the cost of recall for the time period missed and the difficulty of finding the time to put it all together immediately. If possible, would it be possible for you to review the proposed revisions in response to the points raised?

akira-furukawa-stb avatar Jan 22 '25 02:01 akira-furukawa-stb

If possible, would it be possible for you to review the proposed revisions in response to the points raised?

sure, just ping me when the PR is updated

macjohnny avatar Jan 22 '25 08:01 macjohnny

any updates on this PR?

kstanitzok avatar Mar 31 '25 15:03 kstanitzok