29

情况:我有多个传递对象结构的 Web 服务 API 调用。目前,我声明了显式类型以将这些对象结构绑定在一起。为了简单起见,这里有一个例子:

[HttpGet]
[ProducesResponseType(typeof(MyType), 200)]
public MyType TestOriginal()
{
    return new MyType { Speed: 5.0, Distance: 4 };
}

改进:我有很多这样的自定义类,MyType并且很想改用通用容器。我遇到了命名元组,并且可以在我的控制器方法中成功使用它们,如下所示:

[HttpGet]
[ProducesResponseType(typeof((double speed, int distance)), 200)]
public (double speed, int distance) Test()
{
    return (speed: 5.0, distance: 4);
}

我面临的问题Tuple是已解决的类型基于包含这些无意义属性Item1等的底层Item2。示例:

在此处输入图像描述

问题:有没有人找到一种解决方案来将命名元组的名称序列化到我的 JSON 响应中?或者,是否有人找到了一个通用解决方案,该解决方案允许为可以使用的随机结构提供单个类/表示,以便 JSON 响应明确命名它包含的内容。

4

5 回答 5

5

对于序列化响应,只需在操作和自定义合同解析器上使用任何自定义属性(不幸的是,这只是解决方案,但我仍在寻找更优雅的解决方案)。

属性

public class ReturnValueTupleAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        var content = actionExecutedContext?.Response?.Content as ObjectContent;
        if (!(content?.Formatter is JsonMediaTypeFormatter))
        {
            return;
        }

        var names = actionExecutedContext
            .ActionContext
            .ControllerContext
            .ControllerDescriptor
            .ControllerType
            .GetMethod(actionExecutedContext.ActionContext.ActionDescriptor.ActionName)
            ?.ReturnParameter
            ?.GetCustomAttribute<TupleElementNamesAttribute>()
            ?.TransformNames;

        var formatter = new JsonMediaTypeFormatter
        {
            SerializerSettings =
            {
                ContractResolver = new ValueTuplesContractResolver(names),
            },
        };

        actionExecutedContext.Response.Content = new ObjectContent(content.ObjectType, content.Value, formatter);
    }
}

合同解析器

public class ValueTuplesContractResolver : CamelCasePropertyNamesContractResolver
{
    private IList<string> _names;

    public ValueTuplesContractResolver(IList<string> names)
    {
        _names = names;
    }

    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        var properties = base.CreateProperties(type, memberSerialization);
        if (type.Name.Contains(nameof(ValueTuple)))
        {
            for (var i = 0; i < properties.Count; i++)
            {
                properties[i].PropertyName = _names[i];
            }

            _names = _names.Skip(properties.Count).ToList();
        }

        return properties;
    }
}

用法

[ReturnValueTuple]
[HttpGet]
[Route("types")]
public IEnumerable<(int id, string name)> GetDocumentTypes()
{
    return ServiceContainer.Db
        .DocumentTypes
        .AsEnumerable()
        .Select(dt => (dt.Id, dt.Name));
}

这个返回下一个 JSON:

[  
   {  
      "id":0,
      "name":"Other"
   },
   {  
      "id":1,
      "name":"Shipping Document"
   }
]

这里是Swagger UI的解决方案:

public class SwaggerValueTupleFilter : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        var action = apiDescription.ActionDescriptor;
        var controller = action.ControllerDescriptor.ControllerType;
        var method = controller.GetMethod(action.ActionName);
        var names = method?.ReturnParameter?.GetCustomAttribute<TupleElementNamesAttribute>()?.TransformNames;
        if (names == null)
        {
            return;
        }

        var responseType = apiDescription.ResponseDescription.DeclaredType;
        FieldInfo[] tupleFields;
        var props = new Dictionary<string, string>();
        var isEnumer = responseType.GetInterface(nameof(IEnumerable)) != null;
        if (isEnumer)
        {
            tupleFields = responseType
                .GetGenericArguments()[0]
                .GetFields();
        }
        else
        {
            tupleFields = responseType.GetFields();
        }

        for (var i = 0; i < tupleFields.Length; i++)
        {
            props.Add(names[i], tupleFields[i].FieldType.GetFriendlyName());
        }

        object result;
        if (isEnumer)
        {
            result = new List<Dictionary<string, string>>
            {
                props,
            };
        }
        else
        {
            result = props;
        }

        operation.responses.Clear();
        operation.responses.Add("200", new Response
        {
            description = "OK",
            schema = new Schema
            {
                example = result,
            },
        });
    }
于 2019-07-03T07:00:36.810 回答
4

您有一些出价冲突的要求

问题:

我有很多这样的自定义类,MyType并且很想改用通用容器

评论:

但是,我必须在 ProducesResponseType 属性中声明什么类型才能显式公开我返回的内容

基于上述 - 你应该留在你已经拥有的类型。这些类型在您的代码中为其他开发人员/读者或几个月后为您自己提供了有价值的文档。

从可读性的角度

[ProducesResponseType(typeof(Trip), 200)]

那时会更好

[ProducesResponseType(typeof((double speed, int distance)), 200)]

从可维护性的角度来看,
添加/删除属性只需要在一个地方完成。使用通用方法时,您还需要记住更新属性。

于 2017-08-29T07:42:10.510 回答
3

在您的情况下使用命名元组的问题在于它们只是语法糖

如果您检查命名和未命名元组文档,您会发现部分:

这些同义词由编译器和语言处理,以便您可以有效地使用命名元组。IDE 和编辑器可以使用 Roslyn API 读取这些语义名称。您可以在同一程序集中的任何位置通过这些语义名称引用命名元组的元素。生成编译输出时,编译器会将您定义的名称替换为 Item* 等效项。已编译的 Microsoft 中间语言 (MSIL) 不包括您为这些元素指定的名称。

因此,当您在运行时而不是在编译期间进行序列化时,您会遇到问题,并且您想使用在编译期间丢失的信息。可以设计自定义序列化程序,在编译之前用一些代码初始化以记住命名的元组名称,但我想这种复杂性对于这个例子来说太多了。

于 2019-07-26T09:51:57.307 回答
2

请改用匿名对象。

(double speed, int distance) = (5.0, 4);
return new { speed, distance };
于 2021-02-26T13:27:01.580 回答
1

最简单的解决方案是使用dynamic代码,即 C# 的ExpandoObject以您希望 API 具有的格式包装您的响应

    public JsonResult<ExpandoObject> GetSomething(int param)
    {
        var (speed, distance) = DataLayer.GetData(param);
        dynamic resultVM = new ExpandoObject();
        resultVM.speed= speed;
        resultVM.distance= distance;
        return Json(resultVM);
    }

“”的返回类型GetData

(decimal speed, int distance)

这会以您期望的方式给出 Json 响应

于 2020-09-14T05:51:48.673 回答