/// <summary> /// Serialize <see cref="OpenApiMediaType"/> to Open Api v3.0. /// </summary> public void SerializeAsV3(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } writer.WriteStartObject(); // schema writer.WriteOptionalObject(OpenApiConstants.Schema, Schema, (w, s) => s.SerializeAsV3(w)); // example writer.WriteOptionalObject(OpenApiConstants.Example, Example, (w, e) => w.WriteAny(e)); // examples writer.WriteOptionalMap(OpenApiConstants.Examples, Examples, (w, e) => e.SerializeAsV3(w)); // encoding writer.WriteOptionalMap(OpenApiConstants.Encoding, Encoding, (w, e) => e.SerializeAsV3(w)); // extensions writer.WriteExtensions(Extensions); writer.WriteEndObject(); }
/// <summary> /// Serialize <see cref="OpenApiDocument"/> to Open Api v2.0. /// </summary> public void SerializeAsV2(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } writer.WriteStartObject(); // swagger writer.WriteProperty(OpenApiConstants.Swagger, "2.0"); // info writer.WriteRequiredObject(OpenApiConstants.Info, Info, (w, i) => i.SerializeAsV2(w)); // host, basePath, schemes, consumes, produces WriteHostInfoV2(writer, Servers); // paths writer.WriteRequiredObject(OpenApiConstants.Paths, Paths, (w, p) => p.SerializeAsV2(w)); // definitions writer.WriteOptionalMap(OpenApiConstants.Definitions, Components?.Schemas, (w, s) => s.SerializeAsV2WithoutReference(w)); // parameters writer.WriteOptionalMap(OpenApiConstants.Parameters, Components?.Parameters, (w, p) => p.SerializeAsV2WithoutReference(w)); // responses writer.WriteOptionalMap(OpenApiConstants.Responses, Components?.Responses, (w, r) => r.SerializeAsV2WithoutReference(w)); // securityDefinitions writer.WriteOptionalMap(OpenApiConstants.SecurityDefinitions, Components?.SecuritySchemes, (w, s) => s.SerializeAsV2WithoutReference(w)); // security writer.WriteOptionalCollection( OpenApiConstants.Security, SecurityRequirements, (w, s) => s.SerializeAsV2(w)); // tags writer.WriteOptionalCollection(OpenApiConstants.Tags, Tags, (w, t) => t.SerializeAsV2WithoutReference(w)); // externalDocs writer.WriteOptionalObject(OpenApiConstants.ExternalDocs, ExternalDocs, (w, e) => e.SerializeAsV2(w)); // extensions writer.WriteExtensions(Extensions); writer.WriteEndObject(); }
/// <summary> /// Serialize to OpenAPI V3 document without using reference. /// </summary> public void SerializeAsV3WithoutReference(IOpenApiWriter writer) { writer.WriteStartObject(); // name writer.WriteProperty(OpenApiConstants.Name, Name); // in writer.WriteProperty(OpenApiConstants.In, In.GetDisplayName()); // description writer.WriteProperty(OpenApiConstants.Description, Description); // required writer.WriteProperty(OpenApiConstants.Required, Required, false); // deprecated writer.WriteProperty(OpenApiConstants.Deprecated, Deprecated, false); // allowEmptyValue writer.WriteProperty(OpenApiConstants.AllowEmptyValue, AllowEmptyValue, false); // style writer.WriteProperty(OpenApiConstants.Style, Style?.GetDisplayName()); // explode writer.WriteProperty(OpenApiConstants.Explode, Explode, false); // allowReserved writer.WriteProperty(OpenApiConstants.AllowReserved, AllowReserved, false); // schema writer.WriteOptionalObject(OpenApiConstants.Schema, Schema, (w, s) => s.SerializeAsV3(w)); // example writer.WriteOptionalObject(OpenApiConstants.Example, Example, (w, s) => w.WriteAny(s)); // examples writer.WriteOptionalMap(OpenApiConstants.Examples, Examples, (w, e) => e.SerializeAsV3(w)); // content writer.WriteOptionalMap(OpenApiConstants.Content, Content, (w, c) => c.SerializeAsV3(w)); // extensions writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi3_0); writer.WriteEndObject(); }
/// <summary> /// Serialize <see cref="OpenApiExternalDocs"/> to Open Api v3.0. /// </summary> public void SerializeAsV3(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull("writer"); } writer.WriteStartObject(); // contentType writer.WriteProperty(OpenApiConstants.ContentType, ContentType); // headers writer.WriteOptionalMap(OpenApiConstants.Headers, Headers, (w, h) => h.SerializeAsV3(w)); // style writer.WriteProperty(OpenApiConstants.Style, Style?.GetDisplayName()); // explode writer.WriteProperty(OpenApiConstants.Explode, Explode, false); // allowReserved writer.WriteProperty(OpenApiConstants.AllowReserved, AllowReserved, false); // extensions writer.WriteExtensions(Extensions); writer.WriteEndObject(); }
public virtual void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion) { writer.WriteStartObject(); WriteBody(writer, specVersion); writer.WriteOptionalMap("children", Children, specVersion); 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(); }
/// <summary> /// Serialize <see cref="OpenApiOperation"/> to Open Api v3.0. /// </summary> public void SerializeAsV3(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } writer.WriteStartObject(); // tags writer.WriteOptionalCollection( OpenApiConstants.Tags, Tags, (w, t) => { t.SerializeAsV2(w); }); // summary writer.WriteProperty(OpenApiConstants.Summary, Summary); // description writer.WriteProperty(OpenApiConstants.Description, Description); // externalDocs writer.WriteOptionalObject(OpenApiConstants.ExternalDocs, ExternalDocs, (w, e) => e.SerializeAsV3(w)); // operationId writer.WriteProperty(OpenApiConstants.OperationId, OperationId); // parameters writer.WriteOptionalCollection(OpenApiConstants.Parameters, Parameters, (w, p) => p.SerializeAsV3(w)); // requestBody writer.WriteOptionalObject(OpenApiConstants.RequestBody, RequestBody, (w, r) => r.SerializeAsV3(w)); // responses writer.WriteRequiredObject(OpenApiConstants.Responses, Responses, (w, r) => r.SerializeAsV3(w)); // callbacks writer.WriteOptionalMap(OpenApiConstants.Callbacks, Callbacks, (w, c) => c.SerializeAsV3(w)); // deprecated writer.WriteProperty(OpenApiConstants.Deprecated, Deprecated, false); // security writer.WriteOptionalCollection(OpenApiConstants.Security, Security, (w, s) => s.SerializeAsV3(w)); // servers writer.WriteOptionalCollection(OpenApiConstants.Servers, Servers, (w, s) => s.SerializeAsV3(w)); // specification extensions writer.WriteExtensions(Extensions); writer.WriteEndObject(); }
/// <summary> /// Writes this endpoint description to an OpenAPI document. /// </summary> /// <param name="writer">The write to write to.</param> /// <param name="specVersion">The OpenAPI Spec version to use.</param> protected virtual void WriteBody(IOpenApiWriter writer, OpenApiSpecVersion specVersion) { if (!string.IsNullOrEmpty(Type)) { writer.WriteProperty("type", Type); } writer.WriteProperty("description", Description); writer.WriteProperty("uri", Uri); writer.WriteOptionalMap("children", Children, specVersion); }
/// <summary> /// Serialize to OpenAPI V3 document without using reference. /// </summary> public void SerializeAsV3WithoutReference(IOpenApiWriter writer) { writer.WriteStartObject(); // description writer.WriteProperty(OpenApiConstants.Description, Description); // headers writer.WriteOptionalMap(OpenApiConstants.Headers, Headers, (w, h) => h.SerializeAsV3(w)); // content writer.WriteOptionalMap(OpenApiConstants.Content, Content, (w, c) => c.SerializeAsV3(w)); // links writer.WriteOptionalMap(OpenApiConstants.Links, Links, (w, l) => l.SerializeAsV3(w)); // extension writer.WriteExtensions(Extensions); writer.WriteEndObject(); }
private static void WriteOAuthFlowForV2(IOpenApiWriter writer, string flowValue, OpenApiOAuthFlow flow) { // flow writer.WriteProperty(OpenApiConstants.Flow, flowValue); // authorizationUrl writer.WriteProperty(OpenApiConstants.AuthorizationUrl, flow.AuthorizationUrl?.ToString()); // tokenUrl writer.WriteProperty(OpenApiConstants.TokenUrl, flow.TokenUrl?.ToString()); // scopes writer.WriteOptionalMap(OpenApiConstants.Scopes, flow.Scopes, (w, s) => w.WriteValue(s)); }
/// <summary> /// Serialize <see cref="OpenApiComponents"/> to Open Api v3.0. /// </summary> public void SerializeAsV3(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } writer.WriteStartObject(); // schemas writer.WriteOptionalMap(OpenApiConstants.Schemas, Schemas, (w, s) => s.SerializeAsV3WithoutReference(w)); // responses writer.WriteOptionalMap(OpenApiConstants.Responses, Responses, (w, r) => r.SerializeAsV3WithoutReference(w)); // parameters writer.WriteOptionalMap(OpenApiConstants.Parameters, Parameters, (w, p) => p.SerializeAsV3WithoutReference(w)); // examples writer.WriteOptionalMap(OpenApiConstants.Examples, Examples, (w, e) => e.SerializeAsV3WithoutReference(w)); // requestBodies writer.WriteOptionalMap(OpenApiConstants.RequestBodies, RequestBodies, (w, r) => r.SerializeAsV3WithoutReference(w)); // headers writer.WriteOptionalMap(OpenApiConstants.Headers, Headers, (w, h) => h.SerializeAsV3WithoutReference(w)); // securitySchemes writer.WriteOptionalMap(OpenApiConstants.SecuritySchemes, SecuritySchemes, (w, s) => s.SerializeAsV3WithoutReference(w)); // links writer.WriteOptionalMap(OpenApiConstants.Links, Links, (w, link) => link.SerializeAsV3WithoutReference(w)); // callbacks writer.WriteOptionalMap(OpenApiConstants.Callbacks, Callbacks, (w, c) => c.SerializeAsV3WithoutReference(w)); // extensions writer.WriteExtensions(Extensions); writer.WriteEndObject(); }
/// <summary> /// Serialize <see cref="OpenApiDiscriminator"/> to Open Api v3.0 /// </summary> public void SerializeAsV3(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } writer.WriteStartObject(); // propertyName writer.WriteProperty(OpenApiConstants.PropertyName, PropertyName); // mapping writer.WriteOptionalMap(OpenApiConstants.Mapping, Mapping, (w, s) => w.WriteValue(s)); writer.WriteEndObject(); }
/// <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(); }
/// <summary> /// Serialize <see cref="OpenApiServer"/> to Open Api v3.0 /// </summary> public void SerializeAsV3(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } writer.WriteStartObject(); // url writer.WriteProperty(OpenApiConstants.Url, Url); // description writer.WriteProperty(OpenApiConstants.Description, Description); // variables writer.WriteOptionalMap(OpenApiConstants.Variables, Variables, (w, v) => v.SerializeAsV3(w)); // specification extensions writer.WriteExtensions(Extensions); writer.WriteEndObject(); }
/// <summary> /// Serialize to OpenAPI V3 document without using reference. /// </summary> public void SerializeAsV3WithoutReference(IOpenApiWriter writer) { writer.WriteStartObject(); // operationRef writer.WriteProperty(OpenApiConstants.OperationRef, OperationRef); // operationId writer.WriteProperty(OpenApiConstants.OperationId, OperationId); // parameters writer.WriteOptionalMap(OpenApiConstants.Parameters, Parameters, (w, p) => p.WriteValue(w)); // requestBody writer.WriteOptionalObject(OpenApiConstants.RequestBody, RequestBody, (w, r) => r.WriteValue(w)); // description writer.WriteProperty(OpenApiConstants.Description, Description); // server writer.WriteOptionalObject(OpenApiConstants.Server, Server, (w, s) => s.SerializeAsV3(w)); writer.WriteEndObject(); }
/// <summary> /// Serialize <see cref="OpenApiDocument"/> to OpenAPI object V2.0. /// </summary> public void SerializeAsV2(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } writer.WriteStartObject(); // swagger writer.WriteProperty(OpenApiConstants.Swagger, "2.0"); // info writer.WriteRequiredObject(OpenApiConstants.Info, Info, (w, i) => i.SerializeAsV2(w)); // host, basePath, schemes, consumes, produces WriteHostInfoV2(writer, Servers); // paths writer.WriteRequiredObject(OpenApiConstants.Paths, Paths, (w, p) => p.SerializeAsV2(w)); // Serialize each referenceable object as full object without reference if the reference in the object points to itself. // If the reference exists but points to other objects, the object is serialized to just that reference. // definitions writer.WriteOptionalMap( OpenApiConstants.Definitions, Components?.Schemas, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Schema && component.Reference.Id == key) { component.SerializeAsV2WithoutReference(w); } else { component.SerializeAsV2(w); } }); // parameters writer.WriteOptionalMap( OpenApiConstants.Parameters, Components?.Parameters, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Parameter && component.Reference.Id == key) { component.SerializeAsV2WithoutReference(w); } else { component.SerializeAsV2(w); } }); // responses writer.WriteOptionalMap( OpenApiConstants.Responses, Components?.Responses, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Response && component.Reference.Id == key) { component.SerializeAsV2WithoutReference(w); } else { component.SerializeAsV2(w); } }); // securityDefinitions writer.WriteOptionalMap( OpenApiConstants.SecurityDefinitions, Components?.SecuritySchemes, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.SecurityScheme && component.Reference.Id == key) { component.SerializeAsV2WithoutReference(w); } else { component.SerializeAsV2(w); } }); // security writer.WriteOptionalCollection( OpenApiConstants.Security, SecurityRequirements, (w, s) => s.SerializeAsV2(w)); // tags writer.WriteOptionalCollection(OpenApiConstants.Tags, Tags, (w, t) => t.SerializeAsV2WithoutReference(w)); // externalDocs writer.WriteOptionalObject(OpenApiConstants.ExternalDocs, ExternalDocs, (w, e) => e.SerializeAsV2(w)); // extensions writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi2_0); writer.WriteEndObject(); }
/// <summary> /// Serialize to OpenAPI V3 document without using reference. /// </summary> public void SerializeAsV3WithoutReference(IOpenApiWriter writer) { writer.WriteStartObject(); // title writer.WriteProperty(OpenApiConstants.Title, Title); // multipleOf writer.WriteProperty(OpenApiConstants.MultipleOf, MultipleOf); // maximum writer.WriteProperty(OpenApiConstants.Maximum, Maximum); // exclusiveMaximum writer.WriteProperty(OpenApiConstants.ExclusiveMaximum, ExclusiveMaximum); // minimum writer.WriteProperty(OpenApiConstants.Minimum, Minimum); // exclusiveMinimum writer.WriteProperty(OpenApiConstants.ExclusiveMinimum, ExclusiveMinimum); // maxLength writer.WriteProperty(OpenApiConstants.MaxLength, MaxLength); // minLength writer.WriteProperty(OpenApiConstants.MinLength, MinLength); // pattern writer.WriteProperty(OpenApiConstants.Pattern, Pattern); // maxItems writer.WriteProperty(OpenApiConstants.MaxItems, MaxItems); // minItems writer.WriteProperty(OpenApiConstants.MinItems, MinItems); // uniqueItems writer.WriteProperty(OpenApiConstants.UniqueItems, UniqueItems); // maxProperties writer.WriteProperty(OpenApiConstants.MaxProperties, MaxProperties); // minProperties writer.WriteProperty(OpenApiConstants.MinProperties, MinProperties); // required writer.WriteOptionalCollection(OpenApiConstants.Required, Required, (w, s) => w.WriteValue(s)); // enum writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (nodeWriter, s) => nodeWriter.WriteAny(s)); // type writer.WriteProperty(OpenApiConstants.Type, Type); // allOf writer.WriteOptionalCollection(OpenApiConstants.AllOf, AllOf, (w, s) => s.SerializeAsV3(w)); // anyOf writer.WriteOptionalCollection(OpenApiConstants.AnyOf, AnyOf, (w, s) => s.SerializeAsV3(w)); // oneOf writer.WriteOptionalCollection(OpenApiConstants.OneOf, OneOf, (w, s) => s.SerializeAsV3(w)); // not writer.WriteOptionalObject(OpenApiConstants.Not, Not, (w, s) => s.SerializeAsV3(w)); // items writer.WriteOptionalObject(OpenApiConstants.Items, Items, (w, s) => s.SerializeAsV3(w)); // properties writer.WriteOptionalMap(OpenApiConstants.Properties, Properties, (w, s) => s.SerializeAsV3(w)); // additionalProperties writer.WriteOptionalObject( OpenApiConstants.AdditionalProperties, AdditionalProperties, (w, s) => s.SerializeAsV3(w)); // description writer.WriteProperty(OpenApiConstants.Description, Description); // format writer.WriteProperty(OpenApiConstants.Format, Format); // default writer.WriteOptionalObject(OpenApiConstants.Default, Default, (w, d) => w.WriteAny(d)); // nullable writer.WriteProperty(OpenApiConstants.Nullable, Nullable, false); // discriminator writer.WriteOptionalObject(OpenApiConstants.Discriminator, Discriminator, (w, s) => s.SerializeAsV3(w)); // readOnly writer.WriteProperty(OpenApiConstants.ReadOnly, ReadOnly, false); // writeOnly writer.WriteProperty(OpenApiConstants.WriteOnly, WriteOnly, false); // xml writer.WriteOptionalObject(OpenApiConstants.Xml, Xml, (w, s) => s.SerializeAsV2(w)); // externalDocs writer.WriteOptionalObject(OpenApiConstants.ExternalDocs, ExternalDocs, (w, s) => s.SerializeAsV3(w)); // example writer.WriteOptionalObject(OpenApiConstants.Example, Example, (w, e) => w.WriteAny(e)); // deprecated writer.WriteProperty(OpenApiConstants.Deprecated, Deprecated, false); // extensions writer.WriteExtensions(Extensions); writer.WriteEndObject(); }
/// <summary> /// Serialize <see cref="OpenApiComponents"/> to Open Api v3.0. /// </summary> public void SerializeAsV3(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } // If references have been inlined we don't need the to render the components section // however if they have cycles, then we will need a component rendered if (writer.GetSettings().ReferenceInline != ReferenceInlineSetting.DoNotInlineReferences) { var loops = writer.GetSettings().LoopDetector.Loops; writer.WriteStartObject(); if (loops.TryGetValue(typeof(OpenApiSchema), out List <object> schemas)) { var openApiSchemas = schemas.Cast <OpenApiSchema>().Distinct().ToList() .ToDictionary <OpenApiSchema, string>(k => k.Reference.Id); writer.WriteOptionalMap( OpenApiConstants.Schemas, Schemas, (w, key, component) => { component.SerializeAsV3WithoutReference(w); }); } writer.WriteEndObject(); return; } writer.WriteStartObject(); // Serialize each referenceable object as full object without reference if the reference in the object points to itself. // If the reference exists but points to other objects, the object is serialized to just that reference. // schemas writer.WriteOptionalMap( OpenApiConstants.Schemas, Schemas, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Schema && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // responses writer.WriteOptionalMap( OpenApiConstants.Responses, Responses, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Response && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // parameters writer.WriteOptionalMap( OpenApiConstants.Parameters, Parameters, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Parameter && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // examples writer.WriteOptionalMap( OpenApiConstants.Examples, Examples, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Example && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // requestBodies writer.WriteOptionalMap( OpenApiConstants.RequestBodies, RequestBodies, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.RequestBody && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // headers writer.WriteOptionalMap( OpenApiConstants.Headers, Headers, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Header && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // securitySchemes writer.WriteOptionalMap( OpenApiConstants.SecuritySchemes, SecuritySchemes, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.SecurityScheme && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // links writer.WriteOptionalMap( OpenApiConstants.Links, Links, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Link && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // callbacks writer.WriteOptionalMap( OpenApiConstants.Callbacks, Callbacks, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Callback && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // extensions writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi3_0); writer.WriteEndObject(); }
internal void WriteAsSchemaProperties( IOpenApiWriter writer, IList <string> parentRequiredProperties, string propertyName) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } // format writer.WriteProperty(OpenApiConstants.Format, Format); // title writer.WriteProperty(OpenApiConstants.Title, Title); // description writer.WriteProperty(OpenApiConstants.Description, Description); // default writer.WriteOptionalObject(OpenApiConstants.Default, Default, (w, d) => w.WriteAny(d)); // multipleOf writer.WriteProperty(OpenApiConstants.MultipleOf, MultipleOf); // maximum writer.WriteProperty(OpenApiConstants.Maximum, Maximum); // exclusiveMaximum writer.WriteProperty(OpenApiConstants.ExclusiveMaximum, ExclusiveMaximum); // minimum writer.WriteProperty(OpenApiConstants.Minimum, Minimum); // exclusiveMinimum writer.WriteProperty(OpenApiConstants.ExclusiveMinimum, ExclusiveMinimum); // maxLength writer.WriteProperty(OpenApiConstants.MaxLength, MaxLength); // minLength writer.WriteProperty(OpenApiConstants.MinLength, MinLength); // pattern writer.WriteProperty(OpenApiConstants.Pattern, Pattern); // maxItems writer.WriteProperty(OpenApiConstants.MaxItems, MaxItems); // minItems writer.WriteProperty(OpenApiConstants.MinItems, MinItems); // uniqueItems writer.WriteProperty(OpenApiConstants.UniqueItems, UniqueItems); // maxProperties writer.WriteProperty(OpenApiConstants.MaxProperties, MaxProperties); // minProperties writer.WriteProperty(OpenApiConstants.MinProperties, MinProperties); // required writer.WriteOptionalCollection(OpenApiConstants.Required, Required, (w, s) => w.WriteValue(s)); // enum writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (w, s) => w.WriteAny(s)); // type writer.WriteProperty(OpenApiConstants.Type, Type); // items writer.WriteOptionalObject(OpenApiConstants.Items, Items, (w, s) => s.SerializeAsV2(w)); // allOf writer.WriteOptionalCollection(OpenApiConstants.AllOf, AllOf, (w, s) => s.SerializeAsV2(w)); // properties writer.WriteOptionalMap(OpenApiConstants.Properties, Properties, (w, key, s) => s.SerializeAsV2(w, Required, key)); // additionalProperties writer.WriteOptionalObject( OpenApiConstants.AdditionalProperties, AdditionalProperties, (w, s) => s.SerializeAsV2(w)); // discriminator writer.WriteProperty(OpenApiConstants.Discriminator, Discriminator?.PropertyName); // readOnly // In V2 schema if a property is part of required properties of parent schema, // it cannot be marked as readonly. if (!parentRequiredProperties.Contains(propertyName)) { writer.WriteProperty(name: OpenApiConstants.ReadOnly, value: ReadOnly, defaultValue: false); } // xml writer.WriteOptionalObject(OpenApiConstants.Xml, Xml, (w, s) => s.SerializeAsV2(w)); // externalDocs writer.WriteOptionalObject(OpenApiConstants.ExternalDocs, ExternalDocs, (w, s) => s.SerializeAsV2(w)); // example writer.WriteOptionalObject(OpenApiConstants.Example, Example, (w, e) => w.WriteAny(e)); // extensions writer.WriteExtensions(Extensions); }
/// <summary> /// Writes an optional Open API element map. /// </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="elements">The map values.</param> /// <param name="specVersion">The OpenAPI Spec version.</param> public static void WriteOptionalMap <T>([NotNull] this IOpenApiWriter writer, [NotNull] string name, [NotNull] IDictionary <string, T> elements, OpenApiSpecVersion specVersion) where T : IOpenApiSerializable => writer.WriteOptionalMap(name, elements, (w, v) => v.Serialize(w, specVersion));
/// <summary> /// Serialize <see cref="OpenApiComponents"/> to Open Api v3.0. /// </summary> public void SerializeAsV3(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } writer.WriteStartObject(); // Serialize each referenceable object as full object without reference if the reference in the object points to itself. // If the reference exists but points to other objects, the object is serialized to just that reference. // schemas writer.WriteOptionalMap( OpenApiConstants.Schemas, Schemas, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Schema && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // responses writer.WriteOptionalMap( OpenApiConstants.Responses, Responses, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Response && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // parameters writer.WriteOptionalMap( OpenApiConstants.Parameters, Parameters, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Parameter && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // examples writer.WriteOptionalMap( OpenApiConstants.Examples, Examples, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Example && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // requestBodies writer.WriteOptionalMap( OpenApiConstants.RequestBodies, RequestBodies, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.RequestBody && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // headers writer.WriteOptionalMap( OpenApiConstants.Headers, Headers, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Header && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // securitySchemes writer.WriteOptionalMap( OpenApiConstants.SecuritySchemes, SecuritySchemes, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.SecurityScheme && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // links writer.WriteOptionalMap( OpenApiConstants.Links, Links, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Link && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // callbacks writer.WriteOptionalMap( OpenApiConstants.Callbacks, Callbacks, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Callback && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); } else { component.SerializeAsV3(w); } }); // extensions writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi3_0); writer.WriteEndObject(); }
/// <summary> /// Serialize <see cref="OpenApiDocument"/> to OpenAPI object V2.0. /// </summary> public void SerializeAsV2(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } writer.WriteStartObject(); // swagger writer.WriteProperty(OpenApiConstants.Swagger, "2.0"); // info writer.WriteRequiredObject(OpenApiConstants.Info, Info, (w, i) => i.SerializeAsV2(w)); // host, basePath, schemes, consumes, produces WriteHostInfoV2(writer, Servers); // paths writer.WriteRequiredObject(OpenApiConstants.Paths, Paths, (w, p) => p.SerializeAsV2(w)); // If references have been inlined we don't need the to render the components section // however if they have cycles, then we will need a component rendered if (writer.GetSettings().ReferenceInline != ReferenceInlineSetting.DoNotInlineReferences) { var loops = writer.GetSettings().LoopDetector.Loops; if (loops.TryGetValue(typeof(OpenApiSchema), out List <object> schemas)) { var openApiSchemas = schemas.Cast <OpenApiSchema>().Distinct().ToList() .ToDictionary <OpenApiSchema, string>(k => k.Reference.Id); foreach (var schema in openApiSchemas.Values.ToList()) { FindSchemaReferences.ResolveSchemas(Components, openApiSchemas); } writer.WriteOptionalMap( OpenApiConstants.Definitions, openApiSchemas, (w, key, component) => { component.SerializeAsV2WithoutReference(w); }); } } else { // Serialize each referenceable object as full object without reference if the reference in the object points to itself. // If the reference exists but points to other objects, the object is serialized to just that reference. // definitions writer.WriteOptionalMap( OpenApiConstants.Definitions, Components?.Schemas, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Schema && component.Reference.Id == key) { component.SerializeAsV2WithoutReference(w); } else { component.SerializeAsV2(w); } }); } // parameters writer.WriteOptionalMap( OpenApiConstants.Parameters, Components?.Parameters, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Parameter && component.Reference.Id == key) { component.SerializeAsV2WithoutReference(w); } else { component.SerializeAsV2(w); } }); // responses writer.WriteOptionalMap( OpenApiConstants.Responses, Components?.Responses, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.Response && component.Reference.Id == key) { component.SerializeAsV2WithoutReference(w); } else { component.SerializeAsV2(w); } }); // securityDefinitions writer.WriteOptionalMap( OpenApiConstants.SecurityDefinitions, Components?.SecuritySchemes, (w, key, component) => { if (component.Reference != null && component.Reference.Type == ReferenceType.SecurityScheme && component.Reference.Id == key) { component.SerializeAsV2WithoutReference(w); } else { component.SerializeAsV2(w); } }); // security writer.WriteOptionalCollection( OpenApiConstants.Security, SecurityRequirements, (w, s) => s.SerializeAsV2(w)); // tags writer.WriteOptionalCollection(OpenApiConstants.Tags, Tags, (w, t) => t.SerializeAsV2WithoutReference(w)); // externalDocs writer.WriteOptionalObject(OpenApiConstants.ExternalDocs, ExternalDocs, (w, e) => e.SerializeAsV2(w)); // extensions writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi2_0); writer.WriteEndObject(); }
internal void WriteAsSchemaProperties(IOpenApiWriter writer) { if (writer == null) { throw Error.ArgumentNull(nameof(writer)); } // format writer.WriteProperty(OpenApiConstants.Format, Format); // title writer.WriteProperty(OpenApiConstants.Title, Title); // description writer.WriteProperty(OpenApiConstants.Description, Description); // default writer.WriteOptionalObject(OpenApiConstants.Default, Default, (w, d) => w.WriteAny(d)); // multipleOf writer.WriteProperty(OpenApiConstants.MultipleOf, MultipleOf); // maximum writer.WriteProperty(OpenApiConstants.Maximum, Maximum); // exclusiveMaximum writer.WriteProperty(OpenApiConstants.ExclusiveMaximum, ExclusiveMaximum); // minimum writer.WriteProperty(OpenApiConstants.Minimum, Minimum); // exclusiveMinimum writer.WriteProperty(OpenApiConstants.ExclusiveMinimum, ExclusiveMinimum); // maxLength writer.WriteProperty(OpenApiConstants.MaxLength, MaxLength); // minLength writer.WriteProperty(OpenApiConstants.MinLength, MinLength); // pattern writer.WriteProperty(OpenApiConstants.Pattern, Pattern); // maxItems writer.WriteProperty(OpenApiConstants.MaxItems, MaxItems); // minItems writer.WriteProperty(OpenApiConstants.MinItems, MinItems); // uniqueItems writer.WriteProperty(OpenApiConstants.UniqueItems, UniqueItems); // maxProperties writer.WriteProperty(OpenApiConstants.MaxProperties, MaxProperties); // minProperties writer.WriteProperty(OpenApiConstants.MinProperties, MinProperties); // required writer.WriteOptionalCollection(OpenApiConstants.Required, Required, (w, s) => w.WriteValue(s)); // enum writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (w, s) => w.WriteAny(s)); // type writer.WriteProperty(OpenApiConstants.Type, Type); // items writer.WriteOptionalObject(OpenApiConstants.Items, Items, (w, s) => s.SerializeAsV2(w)); // allOf writer.WriteOptionalCollection(OpenApiConstants.AllOf, AllOf, (w, s) => s.SerializeAsV2(w)); // properties writer.WriteOptionalMap(OpenApiConstants.Properties, Properties, (w, s) => s.SerializeAsV2(w)); // additionalProperties writer.WriteOptionalObject( OpenApiConstants.AdditionalProperties, AdditionalProperties, (w, s) => s.SerializeAsV2(w)); // discriminator writer.WriteProperty(OpenApiConstants.Discriminator, Discriminator?.PropertyName); // readOnly writer.WriteProperty(OpenApiConstants.ReadOnly, ReadOnly, false); // xml writer.WriteOptionalObject(OpenApiConstants.Xml, Xml, (w, s) => s.SerializeAsV2(w)); // externalDocs writer.WriteOptionalObject(OpenApiConstants.ExternalDocs, ExternalDocs, (w, s) => s.SerializeAsV2(w)); // example writer.WriteOptionalObject(OpenApiConstants.Example, Example, (w, e) => w.WriteAny(e)); // extensions writer.WriteExtensions(Extensions); }