0

This is probably very simple to do however i haven't been able to get it working.

I have the following RAML contract

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

Now the thing i cant get to work is that whenever I'm trying to reply with either EpaymentTypeTwo or EpaymentTypeThree the json deserializer thinks its just an EpaymentItem. Unless i specify the type as the one it will be getting back it always deserializes into the baseclass.

Is there a way to make the deserializer understand that the type arriving at im is an inherited class?.

Thanks in advance!

EDIT1: Added deserializer. The serializer cant be shows since its done through a 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;
Borja
  • 11
  • 1
  • 1
  • If you show how you are serializing that would help your question. Did you see the other similar questions, such as https://stackoverflow.com/questions/31184938/jsonserializer-not-serializing-derived-class-properties ? – Mashton Jul 23 '18 at 10:58
  • @Mashton I addded the deserializer, however if what the answer in your link is right maybe its better to avoid the use of a derived class. – Borja Jul 23 '18 at 11:09

0 Answers0