in src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/ObjectTypeVisitor.cs [176:254]
private void ProcessProperties(IOpenApiSchemaAcceptor instance, string schemaName, Dictionary<string, PropertyInfo> properties, NamingStrategy namingStrategy)
{
var schemas = new Dictionary<string, OpenApiSchema>();
var subAcceptor = new OpenApiSchemaAcceptor()
{
Properties = properties,
RootSchemas = instance.RootSchemas,
Schemas = schemas,
};
subAcceptor.Accept(this.VisitorCollection, namingStrategy);
// Add required properties to schema.
var jsonPropertyAttributes = properties.Where(p => !p.Value.GetCustomAttribute<JsonPropertyAttribute>(inherit: false).IsNullOrDefault())
.Select(p => new KeyValuePair<string, JsonPropertyAttribute>(p.Key, p.Value.GetCustomAttribute<JsonPropertyAttribute>(inherit: false)))
.Where(p => p.Value.Required == Required.Always || p.Value.Required == Required.AllowNull);
foreach (var attribute in jsonPropertyAttributes)
{
instance.Schemas[schemaName].Required.Add(attribute.Key);
}
var jsonRequiredAttributes = properties.Where(p => !p.Value.GetCustomAttribute<JsonRequiredAttribute>(inherit: false).IsNullOrDefault())
.Select(p => new KeyValuePair<string, JsonRequiredAttribute>(p.Key, p.Value.GetCustomAttribute<JsonRequiredAttribute>(inherit: false)));
foreach (var attribute in jsonRequiredAttributes)
{
var attributeName = namingStrategy.GetPropertyName(attribute.Key, hasSpecifiedName: false);
if (instance.Schemas[schemaName].Required.Contains(attributeName))
{
continue;
}
instance.Schemas[schemaName].Required.Add(attributeName);
}
var requiredPropertyNames = properties.Where(p => !p.Value.GetCustomAttribute<RequiredAttribute>(inherit: false).IsNullOrDefault())
.Select(p => p.Key);
foreach (var propertyName in requiredPropertyNames)
{
var attributeName = namingStrategy.GetPropertyName(propertyName, hasSpecifiedName: false);
if (instance.Schemas[schemaName].Required.Contains(attributeName))
{
continue;
}
instance.Schemas[schemaName].Required.Add(attributeName);
}
instance.Schemas[schemaName].Properties = subAcceptor.Schemas;
// Adds schemas to the root.
var schemasToBeAdded = subAcceptor.Schemas
.Where(p => !instance.Schemas.Keys.Contains(p.Key))
.Where(p => p.Value.IsOpenApiSchemaObject())
.GroupBy(p => p.Value.Title)
.Select(p => p.First())
.ToDictionary(p => p.Value.Title, p => p.Value);
foreach (var schema in schemasToBeAdded.Where(p => !this._noAddedKeys.Contains(p.Key.ToUpperInvariant())))
{
if (instance.RootSchemas.ContainsKey(schema.Key))
{
continue;
}
instance.RootSchemas.Add(schema.Key, schema.Value);
}
// Removes title of each property.
var subSchemas = instance.Schemas[schemaName].Properties;
subSchemas = subSchemas.Select(p =>
{
p.Value.Title = null;
return new KeyValuePair<string, OpenApiSchema>(p.Key, p.Value);
})
.ToDictionary(p => p.Key, p => p.Value);
instance.Schemas[schemaName].Properties = subSchemas;
}