question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript generated client uses inconsistent casing for property names

See original GitHub issue

For this definition in JSON I have Username and Password properties.

"definitions": {
    "TokenRequest": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "Username": {
          "type": "string"
        },
        "Password": {
          "type": "string"
        }
      }
    },

The angular client I’m generating contains the following interface for an ITokenRequest and (as expected) makes the properties lowercase to conform to JS conventions.

export interface ITokenRequest {
    username?: string | undefined;
    password?: string | undefined;
}

However the init() function does not change the casing and gives me this.username = data["Username"]; and you end up with undefined for all the properties.

export class TokenRequest implements ITokenRequest {
    username?: string | undefined;
    password?: string | undefined;

    constructor(data?: ITokenRequest) {
        if (data) {
            for (var property in data) {
                if (data.hasOwnProperty(property))
                    (<any>this)[property] = (<any>data)[property];
            }
        }
    }

    init(data?: any) {
        if (data) {
            this.username = data["Username"];
            this.password = data["Password"];
        }
    }

    static fromJS(data: any): TokenRequest {
        data = typeof data === 'object' ? data : {};
        let result = new TokenRequest();
        result.init(data);
        return result;
    }

    toJSON(data?: any) {
        data = typeof data === 'object' ? data : {};
        data["Username"] = this.username;
        data["Password"] = this.password;
        return data; 
    }
}

This is with all the latest bits from today using msbuild targets to generate the code. I haven’t used nswag in a couple months, so can’t say exactly when this started.

// Generated using the NSwag toolchain v11.17.19.0 (NJsonSchema v9.10.58.0 (Newtonsoft.Json v9.0.0.0)) (http://NSwag.org)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:4
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
RicoSutercommented, Aug 22, 2018

I think this PR fixes that + makes using the same settings in the middleware and CLI easier: https://github.com/RSuter/NSwag/pull/1430

Not sure yet if the actual implementation has the best possible design

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript generated client uses inconsistent casing for ...
I'm generating a Angular 6 Client with DTO Interfaces. The interface gets generated with uppercase property Name (first letter) instead of all ...
Read more >
Inconsistent property name casing in generated JsonResult
I am using IIS and IE9 for testing. I feel like the response is somehow being cached. There are other places in the...
Read more >
TypeScript's quirks: How inconsistencies make ...
The logic here is that because TypeScript is structurally typed, it only cares about the properties that objects contain (not how they were ......
Read more >
TypeScript's quirks: How inconsistencies make ...
Excess property checks catch a common JavaScript bug (typoed property names), structural static typing matches the dynamic duck typing ...
Read more >
Getting started with the TypeScript satisfies operator
The TypeScript satisfies operator is a new and better approach to type-safe configuration in TypeScript released in TypeScript v4.9.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found