swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.91k stars 6.03k forks source link

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

Open farcaller opened 7 years ago

farcaller commented 7 years ago
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.

wing328 commented 7 years ago

@farcaller based on my understanding, there's no TS lib that can serialize/deserialize between JSON and TS object with proper attribute mapping.

There's an enhancement to TS Angular2 (not node) that can handle deserialization with proper attribute mapping via https://github.com/swagger-api/swagger-codegen/pull/4264

Please pull the latest 2.3.0 to generate the client to see if that meets your needs.

zoltanszekely commented 7 years ago

@wing328 #4264 is for typescript-node, not angular2

wing328 commented 7 years ago

@zoltanszekely yes, you're right. Would you have time to port the enhancement from #4264 to TS Angular2?