in src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs [33:103]
public static IDictionary<string, OpenApiSchema> CreateSchemas(this ODataContext context)
{
Utils.CheckArgumentNull(context, nameof(context));
IDictionary<string, OpenApiSchema> schemas = new Dictionary<string, OpenApiSchema>();
// Each entity type, complex type, enumeration type, and type definition directly
// or indirectly used in the paths field is represented as a name / value pair of the schemas map.
// Ideally this would be driven off the types used in the paths, but in practice, it is simply
// all of the types present in the model.
IEnumerable<IEdmSchemaElement> elements = context.Model.GetAllElements();
foreach (var element in elements)
{
switch (element.SchemaElementKind)
{
case EdmSchemaElementKind.TypeDefinition: // Type definition
{
IEdmType reference = (IEdmType)element;
schemas.Add(reference.FullTypeName(), context.CreateSchemaTypeSchema(reference));
}
break;
}
}
// append the Edm.Spatial
foreach(var schema in context.CreateSpatialSchemas())
{
schemas[schema.Key] = schema.Value;
}
// append the OData errors
foreach(var schema in context.CreateODataErrorSchemas())
{
schemas[schema.Key] = schema.Value;
}
if(context.Settings.EnableDollarCountPath)
schemas[Constants.DollarCountSchemaName] = new OpenApiSchema {
Type = "integer",
Format = "int32"
};
schemas = schemas.Concat(context.GetAllCollectionEntityTypes()
.Select(x => new KeyValuePair<string, OpenApiSchema>(
$"{(x is IEdmEntityType eType ? eType.FullName() : x.FullTypeName())}{Constants.CollectionSchemaSuffix}",
CreateCollectionSchema(context, x)))
.Where(x => !schemas.ContainsKey(x.Key)))
.Concat(context.GetAllCollectionComplexTypes()
.Select(x => new KeyValuePair<string, OpenApiSchema>(
$"{x.FullTypeName()}{Constants.CollectionSchemaSuffix}",
CreateCollectionSchema(context, x)))
.Where(x => !schemas.ContainsKey(x.Key)))
.ToDictionary(x => x.Key, x => x.Value);
if(context.HasAnyNonContainedCollections())
{
schemas[$"String{Constants.CollectionSchemaSuffix}"] = CreateCollectionSchema(context, new OpenApiSchema { Type = "string" }, "string");
schemas[Constants.ReferenceUpdateSchemaName] = new()
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>
{
{"@odata.id", new OpenApiSchema { Type = "string", Nullable = false }},
{"@odata.type", new OpenApiSchema { Type = "string", Nullable = true }},
}
};
}
return schemas;
}