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(); }
public void Write(IOpenApiWriter writer) { writer.WriteStartObject(); if (!string.IsNullOrWhiteSpace(Credentials)) { writer.WriteProperty("credentials", Credentials); } writer.WritePropertyName("responses"); { writer.WriteStartObject(); writer.WritePropertyName("default"); { writer.WriteStartObject(); writer.WriteProperty("statusCode", "200"); writer.WriteEndObject(); } writer.WriteEndObject(); } writer.WriteProperty("uri", LambdaUri); writer.WriteProperty("passthroughBehavior", "when_no_templates"); writer.WriteProperty("httpMethod", HttpMethod); writer.WriteProperty("type", "aws"); writer.WriteEndObject(); }
/// <summary> /// Serialize to OpenAPI V2 document without using reference. /// </summary> public void SerializeAsV2WithoutReference(IOpenApiWriter writer) { writer.WriteStartObject(); // description writer.WriteRequiredProperty(OpenApiConstants.Description, Description); var extensionsClone = new Dictionary <string, IOpenApiExtension>(Extensions); 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 (Content.Values.Any(m => m.Example != null)) { writer.WritePropertyName(OpenApiConstants.Examples); writer.WriteStartObject(); foreach (var mediaTypePair in Content) { if (mediaTypePair.Value.Example != null) { writer.WritePropertyName(mediaTypePair.Key); writer.WriteAny(mediaTypePair.Value.Example); } } writer.WriteEndObject(); } writer.WriteExtensions(mediatype.Value.Extensions, OpenApiSpecVersion.OpenApi2_0); foreach (var key in mediatype.Value.Extensions.Keys) { // The extension will already have been serialized as part of the call above, // so remove it from the cloned collection so we don't write it again. extensionsClone.Remove(key); } } } // headers writer.WriteOptionalMap(OpenApiConstants.Headers, Headers, (w, h) => h.SerializeAsV2(w)); // extension writer.WriteExtensions(extensionsClone, OpenApiSpecVersion.OpenApi2_0); writer.WriteEndObject(); }
private static void WritePropertyInternal <T>(this IOpenApiWriter writer, string name, T element) { CheckArgument(writer, name); writer.WritePropertyName(name); writer.WriteValueInternal(element); }
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>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 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> /// 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 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(); }
/// <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 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); }
/// <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 (Content.Values.Any(m => m.Example != null)) { writer.WritePropertyName(OpenApiConstants.Examples); writer.WriteStartObject(); foreach (var mediaTypePair in Content) { if (mediaTypePair.Value.Example != null) { writer.WritePropertyName(mediaTypePair.Key); writer.WriteAny(mediaTypePair.Value.Example); } } writer.WriteEndObject(); } } } // headers writer.WriteOptionalMap(OpenApiConstants.Headers, Headers, (w, h) => h.SerializeAsV2(w)); // extension writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi2_0); writer.WriteEndObject(); }
private static void WriteDictionaryInternal <T>(this IOpenApiWriter writer, string name, IDictionary <string, T> elements) { CheckArgument(writer, name); writer.WritePropertyName(name); writer.WriteStartObject(); if (elements != null) { foreach (KeyValuePair <string, T> e in elements) { writer.WritePropertyName(e.Key); writer.WriteValueInternal(e.Value); } } writer.WriteEndObject(); }
/// <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); }
private void WriteProperties(IOpenApiWriter writer, JSchema schema) { if (schema.Properties != null) { foreach (var property in schema.Properties) { writer.WritePropertyName(property.Key); Providers.WriteValueOrThrow(writer, property.Value); } } if (schema.AdditionalProperties != null) { foreach (string property in AdditionalPropertiesExampleNames) { writer.WritePropertyName(property); Providers.WriteValueOrThrow(writer, schema.AdditionalProperties); } } }
/// <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> /// Serialize OpenApiObject to writer /// </summary> /// <param name="writer"></param> public void Write(IOpenApiWriter writer) { writer.WriteStartObject(); foreach (var item in this) { writer.WritePropertyName(item.Key); writer.WriteAny(item.Value); } writer.WriteEndObject(); }
/// <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> /// Write the specification extensions /// </summary> /// <param name="writer">The Open API writer.</param> /// <param name="extensions">The specification extensions.</param> /// <param name="specVersion">Version of the OpenAPI specification that that will be output.</param> public static void WriteExtensions(this IOpenApiWriter writer, IDictionary <string, IOpenApiExtension> extensions, OpenApiSpecVersion specVersion) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } if (extensions != null) { foreach (var item in extensions) { writer.WritePropertyName(item.Key); item.Value.Write(writer, specVersion); } } }
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(); }
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(); } }
/// <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); } }
/// <summary> /// Write the required Open API object/element. /// </summary> /// <typeparam name="T">The Open API element type. <see cref="IOpenApiElement"/></typeparam> /// <param name="writer">The Open API writer.</param> /// <param name="name">The property name.</param> /// <param name="value">The property value.</param> /// <param name="action">The proprety value writer action.</param> public static void WriteRequiredObject <T>( this IOpenApiWriter writer, string name, T value, Action <IOpenApiWriter, T> action) where T : IOpenApiElement { CheckArguments(writer, name, action); writer.WritePropertyName(name); if (value != null) { action(writer, value); } else { writer.WriteStartObject(); writer.WriteEndObject(); } }
/// <summary> /// Write property with an action. /// </summary> /// <param name="writer">The writer.</param> /// <param name="name">The property name.</param> /// <param name="valueAction">The value action.</param> public static void WriteProperty(this IOpenApiWriter writer, string name, Action valueAction) { if (writer == null) { throw Error.ArgumentNull("writer"); } if (String.IsNullOrWhiteSpace(name)) { throw Error.ArgumentNullOrEmpty("name"); } if (valueAction == null) { return; } writer.WritePropertyName(name); valueAction(); }
private static void WriteObject(this IOpenApiWriter writer, OpenApiObject entity) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } if (entity == null) { throw Error.ArgumentNull(nameof(entity)); } writer.WriteStartObject(); foreach (var item in entity) { writer.WritePropertyName(item.Key); writer.WriteAny(item.Value); } 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(); }