/// <summary>
        /// Write the <see cref="IOpenApiAny"/> value.
        /// </summary>
        /// <typeparam name="T">The Open API Any type.</typeparam>
        /// <param name="writer">The Open API writer.</param>
        /// <param name="any">The Any value</param>
        public static void WriteAny <T>(this IOpenApiWriter writer, T any) where T : IOpenApiAny
        {
            if (writer == null)
            {
                throw Error.ArgumentNull(nameof(writer));
            }

            if (any == null)
            {
                writer.WriteNull();
                return;
            }

            switch (any.AnyType)
            {
            case AnyType.Array:     // Array
                writer.WriteArray(any as OpenApiArray);
                break;

            case AnyType.Object:     // Object
                writer.WriteObject(any as OpenApiObject);
                break;

            case AnyType.Primitive:     // Primitive
                writer.WritePrimitive(any as IOpenApiPrimitive);
                break;

            case AnyType.Null:     // null
                writer.WriteNull();
                break;

            default:
                break;
            }
        }
        private static void WriteMapInternal <T>(
            this IOpenApiWriter writer,
            string name,
            IDictionary <string, T> elements,
            Action <IOpenApiWriter, string, T> action)
        {
            CheckArguments(writer, name, action);

            writer.WritePropertyName(name);
            writer.WriteStartObject();

            if (elements != null)
            {
                foreach (var item in elements)
                {
                    writer.WritePropertyName(item.Key);
                    if (item.Value != null)
                    {
                        action(writer, item.Key, item.Value);
                    }
                    else
                    {
                        writer.WriteNull();
                    }
                }
            }

            writer.WriteEndObject();
        }
Exemplo n.º 3
0
        public bool TryWriteValue(IOpenApiWriter writer, JSchema schema)
        {
            switch (schema.GetSchemaType())
            {
            case OpenApiSchemaType.Null:
                writer.WriteNull();
                return(true);

            case OpenApiSchemaType.Boolean:
                writer.WriteValue(true);
                return(true);

            case OpenApiSchemaType.Integer:
                writer.WriteValue(GetIntValue(schema));
                return(true);

            case OpenApiSchemaType.Number:
                var rawValue = GetNumberValue(schema).ToString("F", CultureInfo.InvariantCulture);
                writer.WriteRaw(rawValue);
                return(true);

            case OpenApiSchemaType.String:
            case OpenApiSchemaType.Object:
            case OpenApiSchemaType.Array:
            case OpenApiSchemaType.Any:
            case OpenApiSchemaType.Combined:
                return(false);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        private static void WriteCollectionInternal <T>(
            this IOpenApiWriter writer,
            string name,
            IEnumerable <T> elements,
            Action <IOpenApiWriter, T> action)
        {
            CheckArguments(writer, name, action);

            writer.WritePropertyName(name);
            writer.WriteStartArray();
            if (elements != null)
            {
                foreach (var item in elements)
                {
                    if (item != null)
                    {
                        action(writer, item);
                    }
                    else
                    {
                        writer.WriteNull();
                    }
                }
            }

            writer.WriteEndArray();
        }
 /// <summary>
 /// Write required string property.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <param name="name">The property name.</param>
 /// <param name="value">The property value.</param>
 public static void WriteRequiredProperty(this IOpenApiWriter writer, string name, string value)
 {
     CheckArguments(writer, name);
     writer.WritePropertyName(name);
     if (value == null)
     {
         writer.WriteNull();
     }
     else
     {
         writer.WriteValue(value);
     }
 }
Exemplo n.º 6
0
            public bool TryWriteValue(IOpenApiWriter writer, JSchema schema)
            {
                switch (schema.Type)
                {
                case JSchemaType.String:
                    writer.WriteValue("text");
                    return(true);

                case JSchemaType.Number:
                case JSchemaType.Integer:
                    writer.WriteValue(42);
                    return(true);

                case JSchemaType.Boolean:
                    writer.WriteValue(true);
                    return(true);

                case JSchemaType.None:
                    return(true);

                case JSchemaType.Null:
                    writer.WriteNull();
                    return(true);

                case null:
                    return(true);

                case JSchemaType.Object:
                    writer.WriteStartObject();
                    foreach (var(propName, propSchema) in schema.Properties)
                    {
                        writer.WritePropertyName(propName);
                        TryWriteValue(writer, propSchema);
                    }
                    writer.WriteEndObject();
                    return(true);

                case JSchemaType.Array:
                    return(false);

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
Exemplo n.º 7
0
        private static void WritePrimitive(this IOpenApiWriter writer, IOpenApiPrimitive primitive)
        {
            if (writer == null)
            {
                throw Error.ArgumentNull(nameof(writer));
            }

            if (primitive == null)
            {
                throw Error.ArgumentNull(nameof(primitive));
            }

            switch (primitive.PrimitiveType)
            {
            case PrimitiveType.Integer:
                var intValue = (OpenApiInteger)primitive;
                writer.WriteValue(intValue.Value);
                break;

            case PrimitiveType.Long:
                var longValue = (OpenApiLong)primitive;
                writer.WriteValue(longValue.Value);
                break;

            case PrimitiveType.Float:
                var floatValue = (OpenApiFloat)primitive;
                writer.WriteValue(floatValue.Value);
                break;

            case PrimitiveType.Double:
                var doubleValue = (OpenApiDouble)primitive;
                writer.WriteValue(doubleValue.Value);
                break;

            case PrimitiveType.String:
                var stringValue = (OpenApiString)primitive;
                writer.WriteValue(stringValue.Value);
                break;

            case PrimitiveType.Byte:
                var byteValue = (OpenApiByte)primitive;
                writer.WriteValue(byteValue.Value);
                break;

            case PrimitiveType.Binary:
                var binaryValue = (OpenApiBinary)primitive;
                if (binaryValue == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(Convert.ToBase64String(binaryValue.Value));
                }
                break;

            case PrimitiveType.Boolean:
                var boolValue = (OpenApiBoolean)primitive;
                writer.WriteValue(boolValue.Value);
                break;

            case PrimitiveType.Date:
                var dateValue = (OpenApiDate)primitive;
                writer.WriteValue(dateValue.Value);
                break;

            case PrimitiveType.DateTime:
                var dateTimeValue = (OpenApiDateTime)primitive;
                writer.WriteValue(dateTimeValue.Value);
                break;

            case PrimitiveType.Password:
                var passwordValue = (OpenApiPassword)primitive;
                writer.WriteValue(passwordValue.Value);
                break;

            default:
                throw new OpenApiWriterException(
                          string.Format(
                              SRResource.PrimitiveTypeNotSupported,
                              primitive.PrimitiveType));
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Write out content of primitive element
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="specVersion"></param>
        public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
        {
            switch (this.PrimitiveType)
            {
            case PrimitiveType.Integer:
                var intValue = (OpenApiInteger)(IOpenApiPrimitive)this;
                writer.WriteValue(intValue.Value);
                break;

            case PrimitiveType.Long:
                var longValue = (OpenApiLong)(IOpenApiPrimitive)this;
                writer.WriteValue(longValue.Value);
                break;

            case PrimitiveType.Float:
                var floatValue = (OpenApiFloat)(IOpenApiPrimitive)this;
                writer.WriteValue(floatValue.Value);
                break;

            case PrimitiveType.Double:
                var doubleValue = (OpenApiDouble)(IOpenApiPrimitive)this;
                writer.WriteValue(doubleValue.Value);
                break;

            case PrimitiveType.String:
                var stringValue = (OpenApiString)(IOpenApiPrimitive)this;
                writer.WriteValue(stringValue.Value);
                break;

            case PrimitiveType.Byte:
                var byteValue = (OpenApiByte)(IOpenApiPrimitive)this;
                if (byteValue.Value == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(Convert.ToBase64String(byteValue.Value));
                }

                break;

            case PrimitiveType.Binary:
                var binaryValue = (OpenApiBinary)(IOpenApiPrimitive)this;
                if (binaryValue.Value == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(Encoding.UTF8.GetString(binaryValue.Value));
                }

                break;

            case PrimitiveType.Boolean:
                var boolValue = (OpenApiBoolean)(IOpenApiPrimitive)this;
                writer.WriteValue(boolValue.Value);
                break;

            case PrimitiveType.Date:
                var dateValue = (OpenApiDate)(IOpenApiPrimitive)this;
                writer.WriteValue(dateValue.Value);
                break;

            case PrimitiveType.DateTime:
                var dateTimeValue = (OpenApiDateTime)(IOpenApiPrimitive)this;
                writer.WriteValue(dateTimeValue.Value);
                break;

            case PrimitiveType.Password:
                var passwordValue = (OpenApiPassword)(IOpenApiPrimitive)this;
                writer.WriteValue(passwordValue.Value);
                break;

            default:
                throw new OpenApiWriterException(
                          string.Format(
                              SRResource.PrimitiveTypeNotSupported,
                              this.PrimitiveType));
            }
        }