contentful / contentful.net

.NET Library for Contentful's Content Delivery and Management API
MIT License
95 stars 55 forks source link

Serialization behaves differently when select is used in query #93

Closed rangp closed 7 years ago

rangp commented 7 years ago

Hello again,

I stumbled upon another issue when using select statements in a query. I understand that you implemented a way of making fields direct descendants of the root element in order to facilitate easy serialization.

While I think this is a nifty feature, it doesn't seem to work when using a select statement, because in that case the resulting response is not of type "Entry" but of type "Array" so in ContentfulClient line 230

if(grandParent["sys"] != null && grandParent["sys"]["type"]?.ToString() != "Entry")

will skip the restructuring of fields. That makes deserialization inconsistent, because if a select is used the resulting response will have a different structure although it's the same object, just with less properties. Thus now I need to have two c# objects. One for the contentful response with select query and one for the contentful response without select.

Do you think it would be possible to make this more consistent without side effects?

Cheers Peter

rangp commented 7 years ago

Oh, wait. sys.type is null, not "Array", because I didn't select it.. that could be a fix.

Okay that fixes it for me now. I have no idea why I first saw "Array" in my debugging console. It would still be nice to have consistent serialization even when "sys.type" is not included in selects, if that's possible

Roblinde commented 7 years ago

Ah, I see.

I'll experiment with that a little and see if it can be made more consistent.

Out of curiosity, what's the main reason using the select operator? Making sure the response is as slimmed as possible?

rangp commented 7 years ago

Alright, thank you!

The complete scenario and background is a bit complicated but basically I'm loading a slim set of all content types (just one property per type) whenever new content is published in contentful (I implemented a listener for the contentful webhooks) .

Then based on the available types I create route constraints in aspnet mvc. Without using select it would take ages and would be a huge network overhead.

Roblinde commented 7 years ago

I think I've found a simple solution for this, I'll do some more testing and hopefully release an updated version later today.

Roblinde commented 7 years ago

Good morning, the new package should now be available on NuGet.

Hope it works for you.

Cheers!