swagger-api / swagger-api/swagger-codegen

[typescript-fetch] response parser doesn't match the generated model

Open
#4,785 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript help wanted Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Running swagger-codegen with -l typescript-fetch produces code that can't properly parse the responses.

Swagger-codegen version

2.2.1

Swagger declaration file content or url

https://gist.github.com/farcaller/825c3aaae2a26f6883fb3554b4ccece9

Command line used for generation

swagger-codegen generate -i test.swagger.json -l typescript-fetch -o api/

Steps to reproduce

Generate the file, try to use it.

Based on the swagger config, the server will reply with an object that looks like:

{
"underscore_name": "qwe",
"a_string": "abc",
"a_number": 123
}

Which is processed as:

...
if (response.status >= 200 && response.status < 300) {
    return response.json();
} else {
...

i.e., taking the json as is and returning it as a typed MainTestMessage interface. But the defined model looks like:

export interface MainTestMessage {
    "underscoreName"?: string;
    "aString"?: string;
    "aNumber"?: number;
}

Producing incorrect result.

Related issues

Nothing relevant found.

Suggest a Fix

Unless {"modelPropertyNaming":"original"} is passed in the config, the generator must preform keys re-mapping.

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 by running the documented swagger-codegen command with test.swagger.json and inspect the generated TypeScript fetch client and MainTestMessage interface. Compare the response parser's raw JSON keys with the generated model names; done means responses are remapped consistently unless modelPropertyNaming is set to original.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.