[IMPLEMENTED] X-Flatten with Create And Update methods with Squidex.ClientLibrary

I am using Squidex.ClientLibrary in my project and I am able to pass QueryContext.Default.Flatten() with all GET calls so I don’t have to decorate class members with InvariantConverter and that works perfectly fine.
On the other side, when I am trying to do similar with CreateAsync and Update/Patch methods, I am getting deserialization exception. I have decorated class like below and object gets created but I get exception during deserialization of response.

public class TestPro
{
[JsonConverter(typeof(InvariantConverter))]
public string test { get; set; }
}

If I change in above code to have InvariantConverter then POST/PUT calls works fine but then GET calls don’t work as I want all responses to be Flaten so client don’t have to deal with iv property.
Is there a way to pass QueryContext.Default.Flatten() in POST/PUT/PATCH calls when using Squidex.ClientLibrary? How can I get both GET and Create/Update working with same class? I don’t want to create duplicate class one with InvariantConverter and one without. Can you help me solve this issue?

There is an “InvariantWriteConverter” whihc can be used for that.

yes, I already tried to use InvariantWriteConverter but getting deserialization exception. Calls does the job of creating and updating but throws exception on below code. This method is part of InvariantWriteConverter

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return serializer.Deserialize(reader, objectType);
}

Looks like Update/Create method is trying to Deserialize reponse and not happy with InvariantWriteConverter.

Do you also set the context for writes?

That’s what I want to know, how to set context for Create/Update/Patch methods. I don’t see as part of method.

    Task<TEntity> UpdateAsync(TEntity entity, CancellationToken ct = default);
    Task<TEntity> UpdateAsync(string id, TData data, CancellationToken ct = default);

My fault, I am going to add it.

Thanks, it will be great if you can add that. I will wait for this feature in SqiodexClientLibrary. Please add to all POST/PUT methods like Create, Update, Patch etc.

I have added the requested overloads.

Great. thanks. Can you publish changes so I can use them? I pulled Version=7.7.0.0 and still don’t see them.

It is published, but nuget might take a while.

Ok, will check tomorrow.

1 Like

I was able to solve the Create/Update issue with new package. Thanks for quick help. I am facing another issue as now with InvariantWriteConverter decoration my GET calls started returning iv properties. Is there way to not return json with iv properties with below settings? I am passing Flaten=true but still I get iv properties in JSON.

public class TestPro
{
[JsonConverter(typeof(InvariantWriteConverter))]
public string test { get; set; }
}

The only option I have to two exact same class one without any Converter and one with InvariantWriteConverter but that adds lot of work to map one class to other.

There are a few workaround for this:

  1. Do not use Newtonsoft.Json in your API. if you use System.Text.Json, the converter is ignored.
  2. Write a custom converter that detects the context of the serialization somehow.

Thanks, Option 1 works but I am trying option 2 but so far haven’t found any way to resolve this. Still trying to get it working with Newtonsoft.Json

This topic was automatically closed after 2 days. New replies are no longer allowed.