private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext context, IEdmStructuredType structuredType, bool processBase, bool processExample) { Debug.Assert(context != null); Debug.Assert(structuredType != null); if (processBase && structuredType.BaseType != null) { // A structured type with a base type is represented as a Schema Object // that contains the keyword allOf whose value is an array with two items: return(new OpenApiSchema { AllOf = new List <OpenApiSchema> { // 1. a JSON Reference to the Schema Object of the base type new OpenApiSchema { Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = structuredType.BaseType.FullTypeName() } }, // 2. a Schema Object describing the derived type context.CreateStructuredTypeSchema(structuredType, false, false) }, AnyOf = null, OneOf = null, Properties = null, Example = CreateStructuredTypePropertiesExample(context, structuredType) }); } else { // A structured type without a base type is represented as a Schema Object of type object OpenApiSchema schema = new OpenApiSchema { Title = (structuredType as IEdmSchemaElement)?.Name, Type = "object", // Each structural property and navigation property is represented // as a name/value pair of the standard OpenAPI properties object. Properties = context.CreateStructuredTypePropertiesSchema(structuredType), // make others null AllOf = null, OneOf = null, AnyOf = null }; // It optionally can contain the field description, // whose value is the value of the unqualified annotation Core.Description of the structured type. if (structuredType.TypeKind == EdmTypeKind.Complex) { IEdmComplexType complex = (IEdmComplexType)structuredType; schema.Description = context.Model.GetDescriptionAnnotation(complex); } else if (structuredType.TypeKind == EdmTypeKind.Entity) { IEdmEntityType entity = (IEdmEntityType)structuredType; schema.Description = context.Model.GetDescriptionAnnotation(entity); } if (processExample) { schema.Example = CreateStructuredTypePropertiesExample(context, structuredType); } return(schema); } }
private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext context, IEdmStructuredType structuredType, bool processBase, bool processExample, IEnumerable <IEdmStructuredType> derivedTypes = null) { Debug.Assert(context != null); Debug.Assert(structuredType != null); IOpenApiAny example = null; if (context.Settings.ShowSchemaExamples) { example = CreateStructuredTypePropertiesExample(context, structuredType); } if (context.Settings.EnableDiscriminatorValue && derivedTypes == null) { derivedTypes = context.Model.FindAllDerivedTypes(structuredType); } if (processBase && structuredType.BaseType != null) { // The x-ms-discriminator-value extension is added to structured types which are derived types. Dictionary <string, IOpenApiExtension> extension = null; if (context.Settings.EnableDiscriminatorValue && !derivedTypes.Any()) { extension = new Dictionary <string, IOpenApiExtension> { { Constants.xMsDiscriminatorValue, new OpenApiString("#" + structuredType.FullTypeName()) } }; } // A structured type with a base type is represented as a Schema Object // that contains the keyword allOf whose value is an array with two items: return(new OpenApiSchema { Extensions = extension, AllOf = new List <OpenApiSchema> { // 1. a JSON Reference to the Schema Object of the base type new OpenApiSchema { UnresolvedReference = true, Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = structuredType.BaseType.FullTypeName() } }, // 2. a Schema Object describing the derived type context.CreateStructuredTypeSchema(structuredType, false, false, derivedTypes) }, AnyOf = null, OneOf = null, Properties = null, Example = example }); } else { // The discriminator object is added to structured types which have derived types. OpenApiDiscriminator discriminator = null; if (context.Settings.EnableDiscriminatorValue && derivedTypes.Any()) { Dictionary <string, string> mapping = derivedTypes .ToDictionary(x => $"#{x.FullTypeName()}", x => new OpenApiSchema { Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = x.FullTypeName() } }.Reference.ReferenceV3); discriminator = new OpenApiDiscriminator { PropertyName = Constants.OdataType, Mapping = mapping }; } // A structured type without a base type is represented as a Schema Object of type object OpenApiSchema schema = new() { Title = (structuredType as IEdmSchemaElement)?.Name, Type = "object", Discriminator = discriminator, // Each structural property and navigation property is represented // as a name/value pair of the standard OpenAPI properties object. Properties = context.CreateStructuredTypePropertiesSchema(structuredType), // make others null AllOf = null, OneOf = null, AnyOf = null }; if (context.Settings.EnableDiscriminatorValue) { if (!schema.Properties.TryAdd(Constants.OdataType, new OpenApiSchema() { Type = "string", Default = new OpenApiString("#" + structuredType.FullTypeName()), })) { throw new InvalidOperationException( $"Property {Constants.OdataType} is already present in schema {structuredType.FullTypeName()}; verify CSDL."); } schema.Required.Add(Constants.OdataType); } // It optionally can contain the field description, // whose value is the value of the unqualified annotation Core.Description of the structured type. if (structuredType.TypeKind == EdmTypeKind.Complex) { IEdmComplexType complex = (IEdmComplexType)structuredType; schema.Description = context.Model.GetDescriptionAnnotation(complex); } else if (structuredType.TypeKind == EdmTypeKind.Entity) { IEdmEntityType entity = (IEdmEntityType)structuredType; schema.Description = context.Model.GetDescriptionAnnotation(entity); } if (processExample) { schema.Example = example; } return(schema); } }
private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext context, IEdmStructuredType structuredType, bool processBase, bool processExample, IEnumerable <IEdmEntityType> derivedTypes = null) { Debug.Assert(context != null); Debug.Assert(structuredType != null); if (context.Settings.EnableDiscriminatorValue && derivedTypes == null) { derivedTypes = context.Model.FindDirectlyDerivedTypes(structuredType).OfType <IEdmEntityType>(); } if (processBase && structuredType.BaseType != null) { // The x-ms-discriminator-value extension is added to structured types which are derived types. Dictionary <string, IOpenApiExtension> extension = null; if (context.Settings.EnableDiscriminatorValue && !derivedTypes.Any()) { extension = new Dictionary <string, IOpenApiExtension> { { Constants.xMsDiscriminatorValue, new OpenApiString("#" + structuredType.FullTypeName()) } }; } // A structured type with a base type is represented as a Schema Object // that contains the keyword allOf whose value is an array with two items: return(new OpenApiSchema { Extensions = extension, AllOf = new List <OpenApiSchema> { // 1. a JSON Reference to the Schema Object of the base type new OpenApiSchema { Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = structuredType.BaseType.FullTypeName() } }, // 2. a Schema Object describing the derived type context.CreateStructuredTypeSchema(structuredType, false, false, derivedTypes) }, AnyOf = null, OneOf = null, Properties = null, Example = CreateStructuredTypePropertiesExample(context, structuredType) }); } else { // The discriminator object is added to structured types which have derived types. OpenApiDiscriminator discriminator = null; if (context.Settings.EnableDiscriminatorValue && derivedTypes.Any() && structuredType.BaseType != null) { discriminator = new OpenApiDiscriminator { PropertyName = "@odata.type" }; } // A structured type without a base type is represented as a Schema Object of type object OpenApiSchema schema = new OpenApiSchema { Title = (structuredType as IEdmSchemaElement)?.Name, Type = "object", Discriminator = discriminator, // Each structural property and navigation property is represented // as a name/value pair of the standard OpenAPI properties object. Properties = context.CreateStructuredTypePropertiesSchema(structuredType), // make others null AllOf = null, OneOf = null, AnyOf = null }; // It optionally can contain the field description, // whose value is the value of the unqualified annotation Core.Description of the structured type. if (structuredType.TypeKind == EdmTypeKind.Complex) { IEdmComplexType complex = (IEdmComplexType)structuredType; schema.Description = context.Model.GetDescriptionAnnotation(complex); } else if (structuredType.TypeKind == EdmTypeKind.Entity) { IEdmEntityType entity = (IEdmEntityType)structuredType; schema.Description = context.Model.GetDescriptionAnnotation(entity); } if (processExample) { schema.Example = CreateStructuredTypePropertiesExample(context, structuredType); } return(schema); } }