in src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/RecursiveObjectTypeVisitor.cs [81:140]
public override void Visit(IAcceptor acceptor, KeyValuePair<string, Type> type, NamingStrategy namingStrategy, params Attribute[] attributes)
{
var title = namingStrategy.GetPropertyName(type.Value.Name, hasSpecifiedName: false);
var name = this.Visit(acceptor, name: type.Key, title: title, dataType: "object", dataFormat: null, attributes: attributes);
if (name.IsNullOrWhiteSpace())
{
return;
}
if (!this.IsNavigatable(type.Value))
{
return;
}
var instance = acceptor as OpenApiSchemaAcceptor;
if (instance.IsNullOrDefault())
{
return;
}
// Processes non-recursive properties
var properties = type.Value
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => !p.ExistsCustomAttribute<JsonIgnoreAttribute>())
.Where(p => p.PropertyType != type.Value)
.ToDictionary(p => p.GetJsonPropertyName(namingStrategy), p => p);
this.ProcessProperties(instance, name, properties, namingStrategy);
// Processes recursive properties
var recursiveProperties = type.Value
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => !p.ExistsCustomAttribute<JsonIgnoreAttribute>())
.Where(p => p.PropertyType == type.Value)
.ToDictionary(p => p.GetJsonPropertyName(namingStrategy), p => p);
var recursiveSchemas = recursiveProperties.ToDictionary(p => p.Key,
p => new OpenApiSchema()
{
Type = "object",
Reference = new OpenApiReference()
{
Type = ReferenceType.Schema,
Id = p.Value.PropertyType.GetOpenApiReferenceId(isDictionary: false, isList: false, namingStrategy)
}
});
foreach (var recursiveSchema in recursiveSchemas)
{
instance.Schemas[name].Properties.Add(recursiveSchema);
}
// Adds the reference.
var reference = new OpenApiReference()
{
Type = ReferenceType.Schema,
Id = type.Value.GetOpenApiReferenceId(isDictionary: false, isList: false, namingStrategy)
};
instance.Schemas[name].Reference = reference;
}