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(); } }
/// <summary> /// Write a string/number 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 WriteProperty <T>(this IOpenApiWriter writer, string name, T value) where T : struct { CheckArguments(writer, name); writer.WritePropertyName(name); writer.WriteValue(value); }
private void Write(IOpenApiWriter writer, object value) { if (value is string s) { writer.WriteValue(s); } else if (value is IList <object> l) { writer.WriteStartArray(); foreach (var o in l) { Write(writer, o); } writer.WriteEndArray(); } else if (value is IDictionary <object, object> d) { writer.WriteStartObject(); foreach (var o in d) { Write(writer, o); } writer.WriteEndObject(); } else if (value is KeyValuePair <object, object> kvp) { writer.WritePropertyName((string)kvp.Key); Write(writer, kvp.Value); } else { throw new NotSupportedException($"Value: {value}"); } }
/// <summary> /// Write Any object to the given writer. /// </summary> /// <param name="writer">The writer.</param> public void Write(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull("writer"); } // { for json, empty for YAML writer.WriteStartObject(); foreach (var item in this) { writer.WritePropertyName(item.Key); IOpenApiWritable writerElement = item.Value as IOpenApiWritable; if (writerElement != null) { writerElement.Write(writer); } else { writer.WriteValue(item.Value); } } // } for json, empty for YAML writer.WriteEndObject(); }
/// <inheritdoc /> public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (specVersion != OpenApiSpecVersion.OpenApi3_0) { throw new InvalidOperationException("This extension only applies to OpenAPI 3.0!"); } writer.WriteStartArray(); foreach (var enumValue in Enum.GetValues(enumType)) { var enumName = enumValue.ToString(); var field = enumType.GetField(enumName); if (field.IsDefined(typeof(ObsoleteAttribute), false)) { enumName = $"DEPRECATED_{enumName}"; } writer.WriteValue(enumName); } writer.WriteEndArray(); }
/// <summary> /// Write Open API server variable object. /// </summary> /// <param name="writer">The Open API Writer.</param> public virtual void Write(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull("writer"); } // { for JSON, empty for YAML writer.WriteStartObject(); // default writer.WriteRequiredProperty(OpenApiConstants.OpenApiDocDefault, Default); // description writer.WriteOptionalProperty(OpenApiConstants.OpenApiDocDescription, Description); // enums if (Enums != null && Enums.Any()) { writer.WritePropertyName(OpenApiConstants.OpenApiDocEnum); writer.WriteStartArray(); foreach (string item in Enums) { writer.WriteValue(item); } writer.WriteEndArray(); } // specification extensions writer.WriteDictionary(Extensions); // } for JSON, empty for YAML writer.WriteEndObject(); }
/// <summary> /// Serialize <see cref="OpenApiReference"/> to Open Api v2.0. /// </summary> public void SerializeAsV2(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } if (Type == ReferenceType.Tag) { // Write the string value only writer.WriteValue(ReferenceV2); return; } if (Type == ReferenceType.SecurityScheme) { // Write the string as property name writer.WritePropertyName(ReferenceV2); return; } writer.WriteStartObject(); // $ref writer.WriteProperty(OpenApiConstants.DollarRef, ReferenceV2); writer.WriteEndObject(); }
/// <summary> /// Write the boolean property. /// </summary> /// <param name="writer">The writer.</param> /// <param name="name">The property name.</param> /// <param name="value">The property value.</param> /// <param name="defaultValue">The default value.</param> public static void WriteBooleanProperty(this IOpenApiWriter writer, string name, bool value, bool?defaultValue) { if (defaultValue != null && value == defaultValue.Value) { return; } writer.WriteProperty(name, () => writer.WriteValue(value)); }
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(); } }
public bool TryWriteValue(IOpenApiWriter writer, JSchema schema) { if (schema.IsFormattedString("date")) { var value = DateTime.UtcNow.ToString("yyyy-MM-dd"); writer.WriteValue(value); return(true); } else if (schema.IsFormattedString("date-time")) { var value = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"); writer.WriteValue(value); return(true); } else { return(false); } }
public bool TryWriteValue(IOpenApiWriter writer, JSchema schema) { if (!schema.IsFormattedString("base64")) { return(false); } writer.WriteValue(Base64); return(true); }
/// <summary> /// Write a 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 WriteProperty(this IOpenApiWriter writer, string name, string value) { if (value == null) { return; } CheckArguments(writer, name); writer.WritePropertyName(name); writer.WriteValue(value); }
/// <summary> /// Write a boolean property. /// </summary> /// <param name="writer">The writer.</param> /// <param name="name">The property name.</param> /// <param name="value">The property value.</param> /// <param name="defaultValue">The default boolean value.</param> public static void WriteProperty(this IOpenApiWriter writer, string name, bool value, bool defaultValue = false) { if (value == defaultValue) { return; } CheckArguments(writer, name); writer.WritePropertyName(name); writer.WriteValue(value); }
public bool TryWriteValue(IOpenApiWriter writer, JSchema schema) { if (!schema.IsFormattedString("guid")) { return(false); } var guid = Guid.NewGuid().ToString(); writer.WriteValue(guid); return(true); }
private static void WriteValueInternal <T>(this IOpenApiWriter writer, T value) { IOpenApiWritable writableElement = value as IOpenApiWritable; if (writableElement != null) { writableElement.Write(writer); } else { writer.WriteValue(value); } }
/// <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); } }
/// <summary> /// Serialize <see cref="OpenApiTag"/> to Open Api v3.0 /// </summary> public void SerializeAsV3(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } if (Reference != null) { Reference.SerializeAsV3(writer); return; } writer.WriteValue(Name); }
public bool TryWriteValue(IOpenApiWriter writer, JSchema schema) { if (!schema.IsString()) { return(false); } var minLength = (int)(schema.MinimumLength ?? 0); var maxLength = (int)(schema.MaximumLength ?? 10); var text = GetText(minLength, maxLength); writer.WriteValue(text); return(true); }
/// <summary> /// Write <see cref="RuntimeExpressionAnyWrapper"/> /// </summary> public void WriteValue(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } if (_any != null) { writer.WriteAny(_any); } else if (_expression != null) { writer.WriteValue(_expression.Expression); } }
public static void WriteJToken(this IOpenApiWriter writer, JToken token) { // ReSharper disable once SwitchStatementMissingSomeCases switch (token.Type) { case JTokenType.String: case JTokenType.Date: case JTokenType.Guid: case JTokenType.TimeSpan: case JTokenType.Uri: writer.WriteValue(token.ToString()); break; default: writer.WriteRaw(token.ToString()); break; } }
/// <summary> /// Write Open API document to given writer. /// </summary> /// <param name="writer">The writer.</param> public virtual void Write(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull("writer"); } writer.WritePropertyName(Name); if (Value is IOpenApiWritable) { ((IOpenApiWritable)Value).Write(writer); } else { // TODO: writer.WriteValue(Value); } }
/// <inheritdoc /> public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (specVersion != OpenApiSpecVersion.OpenApi3_0) { throw new InvalidOperationException("This extension only applies to OpenAPI 3.0!"); } writer.WriteStartArray(); foreach (var enumValue in Enum.GetValues(enumType)) { writer.WriteValue(enumValue.ToString()); } writer.WriteEndArray(); }
/// <summary> /// Serialize to OpenAPI V2 document without using reference. /// </summary> public void SerializeAsV2WithoutReference(IOpenApiWriter writer) { writer.WriteStartObject(); // description writer.WriteProperty(OpenApiConstants.Description, Description); if (Content != null) { var mediatype = Content.FirstOrDefault(); if (mediatype.Value != null) { // schema writer.WriteOptionalObject( OpenApiConstants.Schema, mediatype.Value.Schema, (w, s) => s.SerializeAsV2(w)); // examples if (mediatype.Value.Example != null) { writer.WritePropertyName(OpenApiConstants.Examples); writer.WriteStartObject(); writer.WritePropertyName(mediatype.Key); writer.WriteValue(mediatype.Value.Example); writer.WriteEndObject(); } } } // headers writer.WriteOptionalMap(OpenApiConstants.Headers, Headers, (w, h) => h.SerializeAsV2(w)); // extension writer.WriteExtensions(Extensions); writer.WriteEndObject(); }
/// <summary> /// Serialize <see cref="OpenApiSecurityRequirement"/> to Open Api v2.0 /// </summary> public void SerializeAsV2(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } writer.WriteStartObject(); foreach (var securitySchemeAndScopesValuePair in this) { var securityScheme = securitySchemeAndScopesValuePair.Key; var scopes = securitySchemeAndScopesValuePair.Value; if (securityScheme.Reference == null) { // Reaching this point means the reference to a specific OpenApiSecurityScheme fails. // We are not able to serialize this SecurityScheme/Scopes key value pair since we do not know what // string to output. continue; } securityScheme.SerializeAsV2(writer); writer.WriteStartArray(); foreach (var scope in scopes) { writer.WriteValue(scope); } writer.WriteEndArray(); } writer.WriteEndObject(); }
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)); } }
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion) => writer.WriteValue(Value.ToString(Format));
/// <summary> /// Serialize <see cref="OpenApiOperation"/> to Open Api v2.0. /// </summary> public void SerializeAsV2(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } writer.WriteStartObject(); // tags writer.WriteOptionalCollection( OpenApiConstants.Tags, Tags, (w, t) => { t.SerializeAsV3(w); }); // summary writer.WriteProperty(OpenApiConstants.Summary, Summary); // description writer.WriteProperty(OpenApiConstants.Description, Description); // externalDocs writer.WriteOptionalObject(OpenApiConstants.ExternalDocs, ExternalDocs, (w, e) => e.SerializeAsV2(w)); // operationId writer.WriteProperty(OpenApiConstants.OperationId, OperationId); IList <OpenApiParameter> parameters; if (Parameters == null) { parameters = new List <OpenApiParameter>(); } else { parameters = new List <OpenApiParameter>(Parameters); } if (RequestBody != null) { // consumes writer.WritePropertyName(OpenApiConstants.Consumes); writer.WriteStartArray(); var consumes = RequestBody.Content.Keys.Distinct().ToList(); foreach (var mediaType in consumes) { writer.WriteValue(mediaType); } writer.WriteEndArray(); // This is form data. We need to split the request body into multiple parameters. if (consumes.Contains("application/x-www-form-urlencoded") || consumes.Contains("multipart/form-data")) { foreach (var property in RequestBody.Content.First().Value.Schema.Properties) { parameters.Add( new OpenApiFormDataParameter { Description = property.Value.Description, Name = property.Key, Schema = property.Value, Required = RequestBody.Content.First().Value.Schema.Required.Contains(property.Key) }); } } else { var bodyParameter = new OpenApiBodyParameter { Description = RequestBody.Description, // V2 spec actually allows the body to have custom name. // Our library does not support this at the moment. Name = "body", Schema = RequestBody.Content.First().Value.Schema, Required = RequestBody.Required }; parameters.Add(bodyParameter); } } if (Responses != null) { var produces = Responses.Where(r => r.Value.Content != null) .SelectMany(r => r.Value.Content?.Keys) .Distinct() .ToList(); if (produces.Any()) { // produces writer.WritePropertyName(OpenApiConstants.Produces); writer.WriteStartArray(); foreach (var mediaType in produces) { writer.WriteValue(mediaType); } writer.WriteEndArray(); } } // parameters // Use the parameters created locally to include request body if exists. writer.WriteOptionalCollection(OpenApiConstants.Parameters, parameters, (w, p) => p.SerializeAsV2(w)); // responses writer.WriteRequiredObject(OpenApiConstants.Responses, Responses, (w, r) => r.SerializeAsV2(w)); // schemes // All schemes in the Servers are extracted, regardless of whether the host matches // the host defined in the outermost Swagger object. This is due to the // inaccessibility of information for that host in the context of an inner object like this Operation. if (Servers != null) { var schemes = Servers.Select( s => { Uri.TryCreate(s.Url, UriKind.RelativeOrAbsolute, out var url); return(url?.Scheme); }) .Where(s => s != null) .Distinct() .ToList(); writer.WriteOptionalCollection(OpenApiConstants.Schemes, schemes, (w, s) => w.WriteValue(s)); } // deprecated writer.WriteProperty(OpenApiConstants.Deprecated, Deprecated, false); // security writer.WriteOptionalCollection(OpenApiConstants.Security, Security, (w, s) => s.SerializeAsV2(w)); // specification extensions writer.WriteExtensions(Extensions); writer.WriteEndObject(); }
/// <summary> /// Serialize <see cref="OpenApiOperation"/> to Open Api v2.0. /// </summary> public void SerializeAsV2(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } writer.WriteStartObject(); // tags writer.WriteOptionalCollection( OpenApiConstants.Tags, Tags, (w, t) => { t.SerializeAsV3(w); }); // summary writer.WriteProperty(OpenApiConstants.Summary, Summary); // description writer.WriteProperty(OpenApiConstants.Description, Description); // externalDocs writer.WriteOptionalObject(OpenApiConstants.ExternalDocs, ExternalDocs, (w, e) => e.SerializeAsV2(w)); // operationId writer.WriteProperty(OpenApiConstants.OperationId, OperationId); IList <OpenApiParameter> parameters; if (Parameters == null) { parameters = new List <OpenApiParameter>(); } else { parameters = new List <OpenApiParameter>(Parameters); } if (RequestBody != null) { // consumes writer.WritePropertyName(OpenApiConstants.Consumes); writer.WriteStartArray(); var consumes = RequestBody.Content.Keys.Distinct().ToList(); foreach (var mediaType in consumes) { writer.WriteValue(mediaType); } writer.WriteEndArray(); // Create a parameter as BodyParameter type and add to Parameters. // This type will be used to populate the In property as "body" when Parameters is serialized. var bodyParameter = new BodyParameter { Description = RequestBody.Description, Schema = RequestBody.Content.First().Value.Schema, Format = new List <string>(consumes) }; parameters.Add(bodyParameter); } if (Responses != null) { var produces = Responses.Where(r => r.Value.Content != null) .SelectMany(r => r.Value.Content?.Keys) .Distinct() .ToList(); if (produces.Any()) { // produces writer.WritePropertyName(OpenApiConstants.Produces); writer.WriteStartArray(); foreach (var mediaType in produces) { writer.WriteValue(mediaType); } writer.WriteEndArray(); } } // parameters // Use the parameters created locally to include request body if exists. writer.WriteOptionalCollection(OpenApiConstants.Parameters, parameters, (w, p) => p.SerializeAsV2(w)); // responses writer.WriteRequiredObject(OpenApiConstants.Responses, Responses, (w, r) => r.SerializeAsV2(w)); // schemes // All schemes in the Servers are extracted, regardless of whether the host matches // the host defined in the outermost Swagger object. This is due to the // inaccessibility of information for that host in the context of an inner object like this Operation. if (Servers != null) { var schemes = Servers.Select( s => { Uri.TryCreate(s.Url, UriKind.RelativeOrAbsolute, out var url); return(url?.Scheme); }) .Where(s => s != null) .Distinct() .ToList(); writer.WriteOptionalCollection(OpenApiConstants.Schemes, schemes, (w, s) => w.WriteValue(s)); } // deprecated writer.WriteProperty(OpenApiConstants.Deprecated, Deprecated, false); // security writer.WriteOptionalCollection(OpenApiConstants.Security, Security, (w, s) => s.SerializeAsV2(w)); // specification extensions writer.WriteExtensions(Extensions); writer.WriteEndObject(); }
/// <summary> /// Write out content of primitive element /// </summary> /// <param name="writer"></param> public void Write(IOpenApiWriter writer) { 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; writer.WriteValue(byteValue.Value); break; case PrimitiveType.Binary: var binaryValue = (OpenApiBinary)(IOpenApiPrimitive)this; writer.WriteValue(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)); } }
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion) { writer.WriteValue(Name); }