Skip to content

JsonConverterAttribute for Query Params #1275

Closed
@boginw

Description

@boginw

Is your feature request related to a problem? Please describe.
Currently, in the serialization and deserialization of request bodies, the JsonConverterAttribute is considered, allowing for custom conversion logic to be applied. However, when it comes to filters, this attribute is ignored. While this behavior is expected since query parameters are not in JSON format, there are cases where it would be useful to apply certain conversions.

Consider the following scenario where a custom-type Wrapper and its corresponding WrapperJsonConverter are used:

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

[JsonConverter(typeof(WrapperJsonConverter))]
class Wrapper
{
    public string Part1 { get; }
    public string Part2 { get; }

    public Wrapper(string parts)
    {
        string[] split = parts.Split(",");
        Part1 = split[0];
        Part2 = split[1];
    }

    public Wrapper(string part1, string part2)
    {
        Part1 = part1;
        Part2 = part2;
    }
    
    public override string ToString() => Part1 + "," + Part2;
}

public class WrapperJsonConverter : JsonConverter<Wrapper>
{
    public override Wrapper Read(
        ref Utf8JsonReader reader,
        Type typeToConvert,
        JsonSerializerOptions options
    )
    {
        return new Wrapper(reader.GetString());
    }

    public override void Write(
        Utf8JsonWriter writer,
        Wrapper value,
        JsonSerializerOptions options
    )
    {
        writer.WriteStringValue(value.ToString());
    }
}

Currently, when new Wrapper("part1", "part2") is serialized and included in a request body, it is correctly converted to "part1,part2". However, if the client attempts to use a query parameter like equals(wrapper, 'part1,part2'), it will encounter a QueryParseException with the message "Failed to convert 'part1,part2' of type 'String' to type 'Wrapper'." This can lead to confusion and frustration, as the only way forward, is the take the fields from Wrapper and adding them wherever needed.

Describe the solution you'd like
To address this limitation, I propose enhancing RuntimeTypeConverter to support custom type conversions for query parameters as well. This feature would enable developers to define their own conversion logic for custom types when processing query parameters, similar to how it's done for request bodies.

Describe alternatives you've considered

An alternative approach to address the serialization and deserialization of custom types in query parameters could involve utilizing the JsonApiResourceDefinition. In this scenario, the Wrapper class could be treated as a resource, and a corresponding JsonApiResourceDefinition could be created. However, since Wrapper isn't a resource, this would not be advisable.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions