Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript client: DTO fields are not initialized #1653

Closed
ili opened this Issue Oct 5, 2018 · 10 comments

Comments

Projects
None yet
2 participants
@ili
Copy link
Contributor

ili commented Oct 5, 2018

Hi!
After updating to current version toolchain (from v11.12.9.0) we'v faced with breaking change - now DTO peoperties are not initialized by default:

Old ( v11.12.9.0):

export class SelectRequestBaseOfInstallationFilter implements ISelectRequestBaseOfInstallationFilter {
   filter: InstallationFilter = new InstallationFilter(); // INITED
   page: Paginator = new Paginator(); //INITED

    // skipped constructor & etc..
}

Current (v11.20.1.0):

export abstract class SelectRequestBaseOfInstallationFilter implements ISelectRequestBaseOfInstallationFilter {
    filter: InstallationFilter; // NOT INITED
    page: Paginator; // NOT INITED

    // skipped constructor & etc..
}
@RSuter

This comment has been minimized.

Copy link
Owner

RSuter commented Oct 5, 2018

Thats probably because the spec has changed. Can you post the old and new spec? Null handling has been improved for openapi 3 output...

@ili

This comment has been minimized.

Copy link
Contributor Author

ili commented Oct 5, 2018

Do you mean swagger.json? It is the same one in both cases.
swagger.txt

@ili ili closed this Oct 5, 2018

@ili ili reopened this Oct 5, 2018

@RSuter

This comment has been minimized.

Copy link
Owner

RSuter commented Oct 5, 2018

I get this typescript which looks fine for me (with TS 2.7 settings):

  • The properties are required (!)
  • The properties are initialized in the ctor when no data is provided
export abstract class SelectRequestBaseOfInstallationFilter implements ISelectRequestBaseOfInstallationFilter {
    filter!: InstallationFilter;
    page!: Paginator;

    constructor(data?: ISelectRequestBaseOfInstallationFilter) {
        if (data) {
            for (var property in data) {
                if (data.hasOwnProperty(property))
                    (<any>this)[property] = (<any>data)[property];
            }
        }
        if (!data) {
            this.filter = new InstallationFilter();
            this.page = new Paginator();
        }
    }
@ili

This comment has been minimized.

Copy link
Contributor Author

ili commented Oct 24, 2018

Sorry for late responce!
Thanks a lot for your comments! Previously I'v tried with TypeScript version 2.4, now with the latest NSWag & setting TypeScriptVersion 2,7 I'v got:

export abstract class SelectRequestBaseOfInstallationFilter implements ISelectRequestBaseOfInstallationFilter {
    filter!: InstallationFilter;
    page!: Paginator;

    constructor(data?: ISelectRequestBaseOfInstallationFilter) {
        if (data) {
            for (var property in data) {
                if (data.hasOwnProperty(property))
                    (<any>this)[property] = (<any>data)[property];
            }
        }
        if (!data) {
            this.page = new Paginator();
        }
    }
   // code skipped
}

In some reason filter property is not initialized.

My options are:
.tools\nswag\dotnet-nswag.dll swagger2tsclient /input:$(OutDir)swagger.json /output:$(ProjectDir)ClientApp\lime\api.ts /RxJsVersion:6,0 /TypeScriptVersion:2,7 /Template:Angular /HttpClass:HttpClient /InjectionTokenType:InjectionToken --core

@ili

This comment has been minimized.

Copy link
Contributor Author

ili commented Nov 1, 2018

Up )
Teammates are angry with me, the point is only one to migrate to the next Angular version :)

Please help :)

@RSuter

This comment has been minimized.

Copy link
Owner

RSuter commented Nov 1, 2018

In some reason filter property is not initialized.

Can you post the declaration of the filter property and filter schema of your spec or the whole spec?

@ili

This comment has been minimized.

Copy link
Contributor Author

ili commented Nov 2, 2018

Here it is swagger.txt

@RSuter

This comment has been minimized.

Copy link
Owner

RSuter commented Nov 2, 2018

Looks good to me:

image

You can run my config:

nswag.zip

@ili

This comment has been minimized.

Copy link
Contributor Author

ili commented Nov 6, 2018

Shame on me!

I'v gave you wrong swagger definition!

Here is one, where the problem is reproduced (this is to the same API but generated with newer NSwag version).
swagger_new.txt

ili added a commit to ili/NSwag that referenced this issue Nov 26, 2018

@ili

This comment has been minimized.

Copy link
Contributor Author

ili commented Feb 13, 2019

Fixed in NSwag Build 1047

@ili ili closed this Feb 13, 2019

RSuter added a commit that referenced this issue Feb 19, 2019

Test for #1653 (#1768)
* Test for #1653

* Remove & Ignore .editorconfig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.