in src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Extensions/OpenApiPayloadAttributeExtensions.cs [28:91]
public static OpenApiMediaType ToOpenApiMediaType<T>(this T attribute, NamingStrategy namingStrategy = null, VisitorCollection collection = null, OpenApiVersionType version = OpenApiVersionType.V2) where T : OpenApiPayloadAttribute
{
attribute.ThrowIfNullOrDefault();
if (namingStrategy.IsNullOrDefault())
{
namingStrategy = new DefaultNamingStrategy();
}
if (collection.IsNullOrDefault())
{
collection = VisitorCollection.CreateInstance();
}
var type = attribute.BodyType;
// Generate schema based on the type.
var schema = collection.PayloadVisit(type, namingStrategy);
// Add deprecated attribute.
if (attribute is OpenApiRequestBodyAttribute)
{
schema.Deprecated = (attribute as OpenApiRequestBodyAttribute).Deprecated;
}
if (attribute is OpenApiResponseWithBodyAttribute)
{
schema.Deprecated = (attribute as OpenApiResponseWithBodyAttribute).Deprecated;
}
// For array and dictionary object, the reference has already been added by the visitor.
if (type.IsReferentialType() && !type.IsOpenApiNullable() && !type.IsOpenApiArray() && !type.IsOpenApiDictionary())
{
var reference = new OpenApiReference()
{
Type = ReferenceType.Schema,
Id = attribute.BodyType.GetOpenApiReferenceId(isDictionary: false, isList: false, namingStrategy)
};
schema.Reference = reference;
}
var mediaType = new OpenApiMediaType() { Schema = schema };
if (attribute.Example.IsNullOrDefault())
{
return mediaType;
}
if (!attribute.Example.HasInterface("IOpenApiExample`1"))
{
return mediaType;
}
var example = (dynamic)Activator.CreateInstance(attribute.Example);
var examples = (IDictionary<string, OpenApiExample>)example.Build(namingStrategy).Examples;
mediaType.Examples = examples;
if (version == OpenApiVersionType.V2)
{
mediaType.Example = examples.First().Value.Value;
}
return mediaType;
}