-
Notifications
You must be signed in to change notification settings - Fork 6k
Description
I'm using Swagger codegen to generate Angular Typescript bindings for a JSON web service. An example of one of my models from the Swagger definition is as follows:
NamePos:
type: object
properties:
Name:
type: string
Position:
type: string
And this is sent over the wire as the following JSON object:
{"Name": "x", "Position": "y"}
However, the TypeScript generator creates the following model:
export class CapitalStructure {
name: string;
position: string;
}
The API directly casts the returned JSON object into this model. What therefore happens, is that the obj.name is undefined, because the correct property name is actually obj.Name, but that's invalid according to to the generated TypeScript bindings.
As a workaround, I set it to use custom templates and in model.mustache, I changed this line:
{{name}}: {{#isEnum}}{{classname}}.{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}};
Into
{{baseName}}: {{#isEnum}}{{classname}}.{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}};
This now generates models with property names that match the JSON (Name, Position), and the generated bindings work as expected.
However, this would break if any of the properties contained a TypeScript reserved word, and also means the generated TypeScript doesn't follow general language conventions. The correct solution would likely to use some attribute maps from the name -> baseName and then perhaps getters/setters, as other language templates do (e.g. Ruby, Python, PHP). An example from the Python generator can be seen here: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/python/model.mustache#L46