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

TypeScrpit init(): A object property is processed as an array of objects. #697

Closed
homam1 opened this issue May 16, 2018 · 15 comments
Closed

Comments

@homam1
Copy link

homam1 commented May 16, 2018

If the model in the API contains a field from type object, it is converting string to an array of charecters during the init.

The DTO in my case is:

public class FilterDto 
    {
        public long FieldId { get; set; }
        public FilterOperator Operator { get; set; }
        public object Value { get; set; }
    }

The generated code is:

export class FilterDto implements IFilterDto {
    fieldId: number | undefined;
    operator: FilterDtoOperator | undefined;
    value: any | undefined;
    viewId: number | undefined;
    id: number | undefined;

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

    init(data?: any) {
        if (data) {
            this.fieldId = data["fieldId"];
            this.operator = data["operator"];
            if (data["value"]) {
                this.value = {};

// HERE IS THE PROBLEM. The string is broken down to an array of char
                for (let key in data["value"]) {
                    if (data["value"].hasOwnProperty(key))
                        this.value[key] = data["value"][key];
                }
            }
            this.viewId = data["viewId"];
            this.id = data["id"];
        }
    }

}

So, when the filter.value is a string, it is generated as an array of char.

@RicoSuter RicoSuter added this to the vNext milestone May 16, 2018
@homam1
Copy link
Author

homam1 commented May 17, 2018

Is there any workaround for this issue? Can I override the init function externally?
@RSuter

@RicoSuter
Copy link
Owner

Hmm, I think there is no easy workaround. I'd say this is a bug as any/object should be just assigned and not looped through the properties. Or add another a check whether the var contains an object (for) or a primitive (assign)?

@homam1
Copy link
Author

homam1 commented May 17, 2018

I see. Is there any way yo override the whole method and write it myself?

@RicoSuter
Copy link
Owner

Maybe by overriding the correct template:
https://github.com/RSuter/NJsonSchema/wiki/Templates

@RicoSuter
Copy link
Owner

@RicoSuter
Copy link
Owner

@RicoSuter
Copy link
Owner

Can you post the generated schema?

@homam1
Copy link
Author

homam1 commented May 21, 2018

I already did in the post. I've posted the generated TypeScript code

@RicoSuter RicoSuter changed the title Problem in serizlizing object. A string is serizlized as an array of objects. TypeScrpit init(): A object property is processed as an array of objects. May 22, 2018
@RicoSuter
Copy link
Owner

... the JSON Schema which is generated from the DTO.

RicoSuter added a commit that referenced this issue May 23, 2018
@RicoSuter
Copy link
Owner

In this test everything looks fine: 21ce1da

Also the generated output from the schema:
image

@homam1
Copy link
Author

homam1 commented May 23, 2018

Actually this happened after an update to the swagger library to the latest version. Can you please try with this schema?

https://workiom-dev.azurewebsites.net/swagger

@RicoSuter
Copy link
Owner

What library are you using?

@homam1
Copy link
Author

homam1 commented May 23, 2018

Swashbuckle.AspNetCore.SwaggerGen, Version=2.4.0.0

@mutzl
Copy link

mutzl commented Jun 11, 2018

Strings are generated as char arrays. But even worse - other types like boolean, are empty!
So this is really a show stopper.

@RicoSuter
Copy link
Owner

How exactly does the Swagger spec for this look like when generated by Swashbuckle? Somehow NJS treats it as dictionary when it's actually an object (objects are directly assigned, dictionaries are copied)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants