Client Library error trying to Flatten DynamicContent

Below is the relevant code I’ve written to return a ContentsResult using the Client Library. I would like to flatten DynamicContent, however I get the following error. What am I doing wrong? The schema for locations is simple and is similar to an address, but has a title field.

JsonSerializationException: Deserialized JSON type ‘Newtonsoft.Json.Linq.JValue’ is not compatible with expected type ‘Newtonsoft.Json.Linq.JObject’. Path ‘items[0].data.title’, line 1, position 221.

private readonly IContentsClient<DynamicContent, DynamicData> locationsClient;

public APIClient(IOptions<SquidexOptions> appOptions)
{
    var options = appOptions.Value;
    var clientManager = new SquidexClientManager(options);

    locationsClient = clientManager.CreateDynamicContentsClient("locations");
}

var locationsResult = await _apiClient.GetLocationsAsync();

public async Task<ContentsResult<DynamicContent, DynamicData>> GetLocationsAsync()
    {
        var query = new ContentQuery {
            Filter = $"data/slug/iv ne 'nowhere'" //trying to create an OData filter to select * so i can use the Flatten ctx below.
        };
        var ctx = QueryContext.Default.Flatten();
        var locations = await locationsClient.GetAsync(query, ctx);

        return locations;
    }

I converted to a Content object and the code works. I guess I’m trying to figure out how to use DynamicData/DynamicContent so I don’t have to change the object model every time I update the Schema. Dynamics seem like a great way to do this, so I would love to find more documentation on using the Client Library with DynamicContent/DynamicData.

If you have a look to the content you will that DynamicData is a Dictionary of string to Object. You could say it is a bug or bad design because it would be better to use JToken here instead of a JObject.

I have pushed a new version to nuget for you, which should fix that.

1 Like

@Sebastian can you tell me why DynamicData has empty schema values? I have content in these fields, but nothing is returned.

What do you show me here? What exactly have you printed out?

@Sebastian - the screenshot is of the response I get from that GetLocationsAsync() call listed in the code above. When I attempt to flatten the data I get empty properties in the result. I created another method that uses strongly typed LocationData instead of DynamicData and get the field values just fine.

I would love to see an example of retrieving DynamicData using the client so I can use dynamic objects. I am proxying these responses to a reactjs front end, so there is little benefit for me to have strongly typed classes in my .net core project. I would like to query the content, return JSON, and consume it in my react site. I have business reasons for proxying the data through web API instead of calling Squidex directly from react.

Hopefully that makes sense.

Yesterday I have written a few tests to ensure that dynamic data works and it seems to be fine: https://github.com/Squidex/squidex-samples/blob/master/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/DynamicContentTests.cs

Perhaps it is a problem with your serializer. ASP.NET Core uses System.Text.Json and the client library uses Newtonsoft.JSON. This could cause issues when you just copy the types over. Because STS does not understand JToken.

If you do not have to manipulate the content, I would stream them over and do not parse them. You have to do it manually but it is relatively easy.

For example I have written a small proxy using Node to demonstrate that: https://github.com/Squidex/squidex-samples/blob/master/node/proxy/app.ts

@Sebastian - this was an issue with JsonResult as the return type in the controller method. Thanks for pointing me in the right direction.

1 Like

Was it the System.Text.Json thing I mentioned?

Yes, sorry for the late reply.

1 Like