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

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

akira-furukawa-stb
Copy link

@akira-furukawa-stb akira-furukawa-stb commented May 23, 2024

It's part of this issue.(#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

  • Read the contribution guidelines.
  • 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.
  • 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.
  • 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)
  • 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
Copy link
Author

akira-furukawa-stb commented May 23, 2024

@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. 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant