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}"); } }
/// <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>Writes the specified writer.</summary> /// <param name="writer">The writer.</param> /// <param name="specVersion">The spec version.</param> public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion) { writer.WriteStartObject(); writer.WriteProperty("name", this.enumType.Name); writer.WriteProperty("modelAsString", this.enumModeling == OasEnumModeling.AsString); writer.WritePropertyName("values"); var items = Enum.GetValues(enumType); writer.WriteStartArray(); foreach (var item in items) { var summary = OasDocHelpers.GetFieldDocumentationSummary(enumType, item.ToString(), this.commentDocs); writer.WriteStartObject(); writer.WriteProperty("value", this.namingPolicy.ConvertName(item.ToString())); writer.WriteProperty("name", this.namingPolicy.ConvertName(item.ToString())); if (!string.IsNullOrWhiteSpace(summary)) { writer.WriteProperty("description", summary); } writer.WriteEndObject(); } writer.WriteEndArray(); writer.WriteEndObject(); }
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 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> /// Write out contents of OpenApiArray to passed writer /// </summary> /// <param name="writer">Instance of JSON or YAML writer.</param> public void Write(IOpenApiWriter writer) { writer.WriteStartArray(); foreach (var item in this) { writer.WriteAny(item); } writer.WriteEndArray(); }
/// <summary> /// Write out contents of OpenApiArray to passed writer /// </summary> /// <param name="writer">Instance of JSON or YAML writer.</param> /// <param name="specVersion">Version of the OpenAPI specification that that will be output.</param> public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion) { writer.WriteStartArray(); foreach (var item in this) { writer.WriteAny(item); } writer.WriteEndArray(); }
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion) { NullGuard.NotNull(writer, nameof(writer)) .NotNull(specVersion, nameof(specVersion)); writer.WriteStartArray(); foreach (IOpenApiExtension errorCode in ErrorCodes) { errorCode.Write(writer, specVersion); } writer.WriteEndArray(); }
public bool TryWriteValue(IOpenApiWriter writer, JSchema schema) { if (!schema.IsArray()) { return(false); } writer.WriteStartArray(); WriteItems(writer, schema); writer.WriteEndArray(); return(true); }
/// <summary> /// Write an array with an action. /// </summary> /// <param name="writer">The writer.</param> /// <param name="arrayAction">The array action.</param> public static void WriteArray(this IOpenApiWriter writer, Action arrayAction) { if (writer == null) { throw Error.ArgumentNull("writer"); } if (arrayAction == null) { throw Error.ArgumentNull("valueAction"); } writer.WriteStartArray(); arrayAction(); writer.WriteEndArray(); }
private static void WriteCollectionInternal <T>(this IOpenApiWriter writer, string name, IEnumerable <T> elements) { CheckArgument(writer, name); writer.WritePropertyName(name); writer.WriteStartArray(); if (elements != null) { foreach (T e in elements) { writer.WriteValueInternal(e); } } writer.WriteEndArray(); }
/// <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(); }
private static void WriteArray(this IOpenApiWriter writer, OpenApiArray array) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } if (array == null) { throw Error.ArgumentNull(nameof(array)); } writer.WriteStartArray(); foreach (var item in array) { writer.WriteAny(item); } writer.WriteEndArray(); }
/// <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(); }
/// <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(); }