Elfocrash / Cosmonaut

🌐 A supercharged Azure CosmosDB .NET SDK with ORM support
https://cosmonaut.readthedocs.io
MIT License
342 stars 44 forks source link

Transfer JSON serializer settings from CosmonautClient to CosmosStore #96

Closed HMoen closed 5 years ago

HMoen commented 5 years ago

Thanks for the most recent update 2.11.1.

In order to re-use the DocumentClient, the following used to work with 2.10.0: var client = new CosmonautClient(documentClient); var store = new CosmosStore<TDocument>(client, "NAME");

Since the most recent update, we have to populate the JsonSerializerSettings property of the cosmos store settings manually with the following since it is null: store.Settings.JsonSerializerSettings = JsonUtil.SerializerSettingsInternal;

This is not a big deal, but it would be ideal if the new private field _serializerSettings value can be used in the CosmosStore ctor to build Settings.

Thanks in advance.

Elfocrash commented 5 years ago

You don't have to populate anything. Leaving it null will pass the responsibility to the DocumentClient to initialize it (which was the previous behavior as well). The CosmosStore passes it to the CosmonautClient which passes it to the DocumentClient and visa versa. Leaving it null will let DocumentClient to initialize it which is used in CosmonautClient which is used by the CosmosStore.

HMoen commented 5 years ago

If we don't populate it, we'll get a null reference exception upon querying using FindAsync with the following stack trace:

at Cosmonaut.Extensions.DocumentEntityExtensions.GetPartitionKeyDefinitionForEntity(Type type, JsonSerializerSettings serializerSettings)

at Cosmonaut.CosmosStore`1.GetRequestOptions(String id, RequestOptions requestOptions)

at Cosmonaut.CosmosStore`1.FindAsync(String id, RequestOptions requestOptions, CancellationToken cancellationToken)

at Cosmonaut.CosmosStore`1.FindAsync(String id, Object partitionKeyValue, CancellationToken cancellationToken)

HMoen commented 5 years ago

When creating the DocumentClient, we initialize it with our custom serializer settings and then use the DC to create the Cosmonaut client.

Elfocrash commented 5 years ago

Ok let me take a closer look at that and get back to you.

Elfocrash commented 5 years ago

Can you provide me with some code to reproduce this since I can't seem to be able to?

HMoen commented 5 years ago

Certainly. I’ll try to get it to you tonight.

Elfocrash commented 5 years ago

Ok I got it. It looks like changing serializerSettings.ContractResolver is DefaultContractResolver resolver to serializerSettings?.ContractResolver is DefaultContractResolver resolver solves the issue since the code will fall back to the previous behaviour.

HMoen commented 5 years ago

Thanks for getting to the bottom of it. We’ll keep the manual add until the next update.

HMoen commented 5 years ago

I'll go ahead close since I see https://github.com/Elfocrash/Cosmonaut/commit/7218e528d0b94bdd7067d9d7cfbbdd4a4083bc64