首页 > 解决方案 > RAML 类型继承 C#

问题描述

这可能很简单,但是我无法让它工作。

我有以下 RAML 合同

types:
  EpaymentItem:
    type: object
    properties:
      Payment: string
  EpaymentTypeTwo:
    type: EpaymentItem
    properties:
      Card: string
  EpaymentTypeThree:
    type: EpaymentItem
    properties:
      Bill: string
  EpaymentDTO:
    type: object
    properties:
      payment: EpaymentItem
      name: string
      address: string
/Epayment:
  /{paymentId}
    get:
      responses:
        200:
          body:
            application/json:
              type: EpaymentDTO

现在我无法开始工作的是,每当我尝试使用 EpaymentTypeTwo 或 EpaymentTypeThree 进行回复时,json 反序列化器都会认为它只是一个 EpaymentItem。除非我将类型指定为它将返回的类型,否则它总是反序列化到基类中。

有没有办法让反序列化器明白到达 im 的类型是一个继承的类?

提前致谢!

EDIT1:添加了解串器。序列化程序无法显示,因为它是通过 IHttpActionResult 完成的。

if (typedContent != null)
                return typedContent;

            IEnumerable<string> values = new List<string>();
            if (RawContent != null && RawContent.Headers != null)
                RawContent.Headers.TryGetValues("Content-Type", out values);

            if (values.Any(hv => hv.ToLowerInvariant().Contains("xml")) &&
                !values.Any(hv => hv.ToLowerInvariant().Contains("json")))
            {
                var task = RawContent.ReadAsStreamAsync();

                var xmlStream = task.GetAwaiter().GetResult();
                typedContent = (EpaymentDTO)new XmlSerializer(typeof(EpaymentDTO)).Deserialize(xmlStream);
            }
            else
            {
                var task =  Formatters != null && Formatters.Any() 
                            ? RawContent.ReadAsAsync<EpaymentDTO>(Formatters).ConfigureAwait(false)
                            : RawContent.ReadAsAsync<EpaymentDTO>().ConfigureAwait(false);

                typedContent = task.GetAwaiter().GetResult();
            }
            return typedContent;

标签: c#jsonraml

解决方案


推荐阅读