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

JsonInheritanceConverter thread safety #206

Closed
neotek opened this issue Sep 29, 2016 · 19 comments
Closed

JsonInheritanceConverter thread safety #206

neotek opened this issue Sep 29, 2016 · 19 comments

Comments

@neotek
Copy link

neotek commented Sep 29, 2016

Hi

Thanks for providing such a nice Swagger/JsonSchema implementation including discriminator support.
I tried to implemented this in a Project. Unfortunately the JsonInheritanceConverter seems not to be thread safe. In Unit Tests I get random exceptions as the discriminator field is not present in the written Json.
Setting the contract.Converter = null; in WriteJson() seems to cause that issue.

Has anyone solved this?

Thanks
Urs

@RicoSuter
Copy link
Owner

RicoSuter commented Sep 29, 2016

This is bad, I could not find another way to implement the converter...

@RicoSuter
Copy link
Owner

We need a fix for that. There are unit tests to check its functionality, so it should be simple to experiment...

@RicoSuter
Copy link
Owner

Why is this running in parallel?

@RicoSuter
Copy link
Owner

Is the serializer reused?

@neotek
Copy link
Author

neotek commented Sep 30, 2016

It runs in parallel as it speeds up unit and integration tests. But this could happen in other parallel scenarios as well.
I'm using it like this: JsonConvert.SerializeObject(entity, Newtonsoft.Json.Formatting.Indented, GetJsonSerializerSettings())
So the JsonSerializerSettings are not reused.

@RicoSuter
Copy link
Owner

So if you call JsonConvert.SerializeObject then you see these problems? I thought that SerializeObject creates a new serializer to prevent these problems... But it seems that it reuses the serializer internally...

My problem: I could not find a way to implement the converter without setting the Converter to null...

@RicoSuter
Copy link
Owner

I think this is the problem:

http://stackoverflow.com/questions/33557737/does-json-net-cache-types-serialization-information

But creating a new resolver each time is not an option because it would degrade the performance dramatically...

@RicoSuter
Copy link
Owner

Ill try something with thread local... But i have to find a solution to reproduce your problem in a unit test first...

@neotek
Copy link
Author

neotek commented Sep 30, 2016

I see. Yes, I also was not able to find a solution that works.
This implementation seems to have the same Problem:
https://github.com/Azure/autorest/blob/45ddc1a84c58f5732a2f77418079c9439f39e892/src/client/Microsoft.Rest.ClientRuntime/Serialization/PolymorphicSerializeJsonConverter.cs

I tried to implement a workaround by defining a property on the base class with the name of the discriminator:

    [JsonProperty]
    public string Kind
    {
        get
        {
            if (string.IsNullOrEmpty(_kind))
            {
                Type type = this.GetType().UnderlyingSystemType;
                _kind = type.Name;

                return type.Name;
            }

            return _kind;
        }
    }

This works for Serialize/Deserialize the data, but when i try to build the swagger.json I get an exception as the field is defined multiple times.
https://github.com/NJsonSchema/NJsonSchema/blob/baaa6a3afd2f2e632ad2ca2cd1a2dd4edc509d34/src/NJsonSchema/Generation/JsonSchemaGenerator.cs#L304

Thanks for your help.

@RicoSuter
Copy link
Owner

RicoSuter commented Sep 30, 2016

The "defined multiple times" problem can easily be fixed...
But I don't like that you have to implement the property yourself...

@RicoSuter
Copy link
Owner

@RicoSuter
Copy link
Owner

I think I have found a solution...

@RicoSuter
Copy link
Owner

62850de

@RicoSuter
Copy link
Owner

Also updated in NSwag: RicoSuter/NSwag@a47fca7

@RicoSuter
Copy link
Owner

RicoSuter commented Sep 30, 2016

Check out NJsonSchema v4.18. Is this working for you?

@neotek
Copy link
Author

neotek commented Oct 1, 2016

Hey, thank you.
For me this solution still does not work as I use nested structures with additional classes that have discriminators.

I extended the Tests here:
https://github.com/neotek/NJsonSchema/commit/6246910f651da348d34d989be33af13feaa880ac

With the new approach all child elements are deserialized to the base class. The JsonInheritanceConverter seems to gets called recursively and is disabled on the child elements.

@RicoSuter
Copy link
Owner

Ok, next workaround :-), now it should work as expected:

97228f2

@RicoSuter
Copy link
Owner

v4.19

@neotek
Copy link
Author

neotek commented Oct 3, 2016

That helped.
No failing unit tests anymore.
Thanks a lot.

@neotek neotek closed this as completed Oct 3, 2016
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

2 participants