OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-angular] Custom typeMapping for file is overriden in generator

Open
#3,341 14 comments 0 reactions 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?
    Foo:
      type: object
      properties:
        msg:
          type: string
          format: binary

The generator produces msg with the type Blob.

class Foo {
    msg: Blob;
}

Regardless of the various mappings I attempt to set, I cannot find the correct mapping declaration to ensure that this is instead mapped to string. The obvious typeMapping file=string should work, but it's overriden in code here: https://github.com/OpenAPITools/openapi-generator/blob/v4.0.2/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java#L282

  • Have you validated the input using an OpenAPI validator (example)?
    Yes

  • What's the version of OpenAPI Generator used?
    4.0.2

  • Have you search for related issues/PRs?
    The keyword blob returns too many deep links into github code. So I looked at the code.

  • What's the actual output vs expected output?

I would like to generate the following:

class Foo {
    msg: string;
}

I'm migrating from the io.swagger tool and would like to avoid breaking changes. Blob breaks existing code.

Suggest a fix

If I were to have a stab at this, I think the complete removal of the overriding method would be enough: https://github.com/OpenAPITools/openapi-generator/blob/v4.0.2/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java#L280

    @Override
    public String getTypeDeclaration(Schema p) {
        if (ModelUtils.isFileSchema(p)) {
            return "Blob";
        } else {
            return super.getTypeDeclaration(p);
        }
}

The correct default behaviour should already be provided with the typeMapping in the constructor:

typeMapping.put("file", "Blob");

However, there is so much going on here, I just can't guesstimate what else this might effect. Such as this method:

    @Override
    public boolean isDataTypeFile(final String dataType) {
        return dataType != null && dataType.equals("Blob");
    }

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

Start in modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java, examining getTypeDeclaration, isDataTypeFile, and the constructor's file mapping. Reproduce the provided binary-property YAML with a custom file=string mapping, then verify that the generated TypeScript uses string while the default remains Blob and file handling is preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, java, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.