The Azure DocumentDB Blog

All about DocumentDB and Logic Apps

Category: API (page 1 of 7)

Invalid Swagger from API App – Tip

Have you ever received an Invalid Swagger 2.0 Error for an API App?

If you are returning the standard status codes, you will find that there are duplicate codes.  This is one cause of the error.

If you edit your SwaggerConfig.cs, as shown below, this will solve the problem

 // In accordance with the built in JsonSerializer, Swashbuckle will, by default, describe enums as integers.
 // You can change the serializer behavior by configuring the StringToEnumConverter globally or for a given
 // enum type. Swashbuckle will honor this change out-of-the-box. However, if you use a different
 // approach to serialize enums as strings, you can also force Swashbuckle to describe them as strings.


Using the UnixDateTimeConverter Class

If you have very looked at the sample for DocumentDB, under the Shared project and in the Until folder, you will see the following class.

namespace DocumentDB.Samples.Shared.Util
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Resources;
    using System.Text;
    using System.Threading.Tasks;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public class UnixDateTimeConverter : DateTimeConverterBase
        private static readonly DateTime UnixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            if (value is DateTime)
                long totalSeconds = (long)((DateTime)value - UnixStartTime).TotalSeconds;
                throw new ArgumentException("value");

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            if (reader.TokenType != Newtonsoft.Json.JsonToken.Integer)
                throw new Exception("Invalid token. Expected integer");

            double totalSeconds = 0;

                totalSeconds = Convert.ToDouble(reader.Value, CultureInfo.InvariantCulture);
                throw new Exception("Invalid double value.");

            return UnixStartTime.AddSeconds(totalSeconds);

The primary use of this class is to be used as a converter type for the JsonConverter Attribute as shown in the following code sample.

  public DateTime CreatedAt { get; set; }

The [JsonConverter(Type)] attribute instructs the JsonSerializer to use the  convert  an object to and from JSON

Besides DocumentDB, many social services use a double data type for DateTime values.  Twitter for one, is a good example.  By decorating your models class properties with this attribute,  your DateTime value will be serialized as a UnixDateTime value in JSON format when writing and from JSON  when reading.

Older posts
%d bloggers like this:
Skip to toolbar