OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript-fetch] Enum JSON serialization as bitwise or of enum types

Open
#11,373 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The request JSON serialization method for model with attribute of type union of enums is generated incorrectly.

Lets look at this code-generated code

export function FormProgressRequestToJSON(value?: FormProgressRequest | null): any {
    if (value === undefined) {
        return undefined;
    }
    if (value === null) {
        return null;
    }
    return {
        'shipping_provider': ShippingProviderEnum | BlankEnum | NullEnumToJSON(value.shipping_provider),
    };
}

Note that shipping_provider is of type oneOf: ShippingProviderEnum | BlankEnum | NullEnum.

Then this must be incorrect since type of shipping_provider is string | null but bitwise or | yields number independently of typeof value.shiping_provider.

ShippingProviderEnum | BlankEnum | NullEnumToJSON(value.shipping_provider)

This always yields number but it does not even take value.shipping_provider into account. Sure I see that NullEnumToJSON does, but as I understands, this function is used to literally convert null enum to json which accidentally by it's implementation may work just fine for me (if there weren't bitwise or-ed).

The expected output may be using ShippingProviderEnumToJSON in combination with ternary operators checking undefined and null values like it's already implemented elsewhere.

export function FormProgressRequestToJSON(value?: FormProgressRequest | null): any {
    if (value === undefined) {
        return undefined;
    }
    if (value === null) {
        return null;
    }
    return {
        'shipping_provider': value.shipping_provider === null ? null : ShippingProviderEnumToJSON(value.shipping_provider),
    };
}

If there is something wrong with my api specification, just point it out please and I might try to change it.

Lastly I just wanted to note that enums work fine if they are not specified as union of multiple enums. Maybe I'm wrong and this behavior is just not supported.

openapi-generator version

5.3.1

OpenAPI declaration file content or url

Here are interesting parts.

FormProgress:
  type: object
  properties:
    shipping_provider:
      nullable: true
      oneOf:
      - $ref: '#/components/schemas/ShippingProviderEnum'
      - $ref: '#/components/schemas/BlankEnum'
      - $ref: '#/components/schemas/NullEnum'

ShippingProviderEnum:
  enum:
  - VALUE_ONE
  - VALUE_TWO
  - VALUE_THREE
  - VALUE_NONE
  type: string

BlankEnum:
  enum:
  - ''

NullEnum:
  enum:
  - null

Full schema can be found here.

Generation Details
openapi-generator-cli generate \
    -i $API_SCHEMA_FILENAME \
    -g typescript-fetch \
    -o $CODEGEN_API_ROOT \
    --additional-properties=supportsES6=true,npmVersion=6.9.0,typescriptThreePlus=true,modelPropertyNaming=snake_case,paramNaming=snake_case,enumPropertyNaming=UPPERCASE
Steps to reproduce

Just run (you may want to adjust output folder).

openapi-generator-cli generate \
    -i https://gist.githubusercontent.com/Krystofee/ad3d67cf81c32e0fa1ef1862ded7b181/raw/2ca664a93846f930bfa1189ae55967662990a4bf/api_schema.yml \
    -g typescript-fetch \
    -o openapi-krystofee-bug-report \
    --additional-properties=supportsES6=true,npmVersion=6.9.0,typescriptThreePlus=true,modelPropertyNaming=snake_case,paramNaming=snake_case,enumPropertyNaming=UPPERCASE

then look into FormProgressRequest.ts at line#63.

Suggested fix

Firstly please review this bug report. If I'm not mistaken, then I might try to submit PR. Any hint will be much appreciated.

Thank you!

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

Run the provided openapi-generator-cli command with the linked schema, then inspect the generated FormProgressRequest.ts around line 63. Compare the union-of-enums serialization with the expected null and enum handling described in the issue. Done means generated typescript-fetch code serializes the selected enum value correctly without bitwise-or behavior, including null values.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, 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.