Baldie47
Baldie47

Reputation: 1264

Problem with types for API request (expects array)

I'm working on a SDK, and I have a request that is being formatted as following:

{
    "maxCount": 5,
    "startDateTime": "0001-01-01T00:00:00+00:00",
    "endDateTime": "9999-12-31T23:59:59.9999999+00:00",
    "clinics": {
        "clinicId": "string",
        "treatments": {
            "treatmentId": "string",
            "treaters": [
                "string",
                "string"
            ]
        }
    }
}

however, the API expects this:

{
    "maxCount": 5,
    "startDateTime": "0001-01-01T00:00:00+00:00",
    "endDateTime": "9999-12-31T23:59:59.9999999+00:00",
    "clinics": [
        {
            "clinicId": "string",
            "treatments": [
                {
                    "treatmentId": "string",
                    "treaters": [
                        "string",
                        "string"
                    ]
                }
            ]
        }
    ]
}

if you spot the difference, the issue is that clinics and treatments should be an array [ ].

now, I should change this in the constructor?

    public record SearchAvailableSlotsByClinicsRequest
    {
        public SearchAvailableSlotsByClinicsRequest(int maxCount, DateTimeOffset startDateTime, DateTimeOffset endDateTime, ClinicTreatment clinics)
        {
            MaxCount = maxCount;
            StartDatetime = startDateTime;
            EndDateTime = endDateTime;
            Clinics = clinics;
        }
        public int MaxCount { get; set; }
        public DateTimeOffset StartDatetime { get; set; }
        public DateTimeOffset EndDateTime { get; set; }
        public ClinicTreatment  Clinics { get; set; }
    }

I'm asking as it seems that if I make the ClinicTreatment and IEnumerable the class that created the clinicTreatment will give error as the object it creates is not an IEnumerable. what am I doing wrong?

    public class ClinicTreatment
    {
        public string ClinicId { get; set; }
        public TreatmentTreater Treatments { get; set; }
    }

and then the clinicTreatment gives problem as the clinicTreatment is not IEnumerable:

var clinicTreatment = new ClinicTreatment {ClinicId = clinic.Id.Id, Treatments = treatmentTreater };

Upvotes: 0

Views: 28

Answers (1)

Yotic
Yotic

Reputation: 153

Do it like this:

    public record SearchAvailableSlotsByClinicsRequest
    {
        public SearchAvailableSlotsByClinicsRequest(int maxCount, DateTimeOffset startDateTime, DateTimeOffset endDateTime, ClinicTreatment clinics)
        {
            MaxCount = maxCount;
            StartDatetime = startDateTime;
            EndDateTime = endDateTime;
            Clinics = clinics;
        }
        public int MaxCount { get; set; }
        public DateTimeOffset StartDatetime { get; set; }
        public DateTimeOffset EndDateTime { get; set; }
        public ClinicTreatment[]  Clinics { get; set; }
    }

And when you need to serialize it, then use not this:

{
   Clinics = yourClinics,
}

Then use this:

{
   Clinics = new Clinics[1]{yourClinics},
}

Upvotes: 1

Related Questions