public void NonNullableBinaryPropertyWithBothMaxLengthAndDefaultValueWorks() { // Arrange ODataContext context = new ODataContext(EdmModelHelper.BasicEdmModel); EdmEntityType entitType = new EdmEntityType("NS", "Entity"); var binaryType = new EdmBinaryTypeReference(EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Binary), false, false, 44); IEdmStructuralProperty property = new EdmStructuralProperty( entitType, "BinaryValue", binaryType, "T0RhdGE"); // Act var schema = context.CreatePropertySchema(property); // Assert Assert.NotNull(schema); Assert.Equal("string", schema.Type); string json = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); Assert.Equal(@"{ ""maxLength"": 44, ""type"": ""string"", ""format"": ""base64url"", ""default"": ""T0RhdGE"" }".ChangeLineBreaks(), json); }
public void NonNullableIntegerPropertyWithDefaultValueWorks() { // Arrange ODataContext context = new ODataContext(EdmModelHelper.BasicEdmModel); EdmEntityType entitType = new EdmEntityType("NS", "Entity"); IEdmStructuralProperty property = new EdmStructuralProperty( entitType, "IntegerValue", EdmCoreModel.Instance.GetInt32(false), "-128"); // Act var schema = context.CreatePropertySchema(property); // Assert Assert.NotNull(schema); Assert.Equal("integer", schema.Type); string json = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); Assert.Equal(@"{ ""maximum"": 2147483647, ""minimum"": -2147483648, ""type"": ""integer"", ""format"": ""int32"", ""default"": -128 }".ChangeLineBreaks(), json); }
public void CreatePropertySchemaForNonNullableEnumPropertyReturnSchema(OpenApiSpecVersion specVersion) { // Arrange IEdmModel model = EdmModelHelper.BasicEdmModel; ODataContext context = new ODataContext(model); context.Settings.OpenApiSpecVersion = specVersion; IEdmEnumType enumType = model.SchemaElements.OfType <IEdmEnumType>().First(e => e.Name == "Color"); EdmEntityType entitType = new EdmEntityType("NS", "Entity"); IEdmProperty property = new EdmStructuralProperty(entitType, "ColorEnumValue", new EdmEnumTypeReference(enumType, false)); // Act var schema = context.CreatePropertySchema(property); Assert.NotNull(schema); string json = schema.SerializeAsJson(specVersion); // Assert if (specVersion == OpenApiSpecVersion.OpenApi2_0) { Assert.Equal(@"{ ""$ref"": ""#/definitions/DefaultNs.Color"" }".ChangeLineBreaks(), json); } else { Assert.Equal(@"{ ""$ref"": ""#/components/schemas/DefaultNs.Color"" }".ChangeLineBreaks(), json); } }
/// <summary> /// Create a map of string/<see cref="OpenApiSchema"/> map for a <see cref="IEdmStructuredType"/>'s all properties. /// </summary> /// <param name="context">The OData context.</param> /// <param name="structuredType">The Edm structured type.</param> /// <returns>The created map of <see cref="OpenApiSchema"/>.</returns> public static IDictionary <string, OpenApiSchema> CreateStructuredTypePropertiesSchema(this ODataContext context, IEdmStructuredType structuredType) { Utils.CheckArgumentNull(context, nameof(context)); Utils.CheckArgumentNull(structuredType, nameof(structuredType)); // The name is the property name, the value is a Schema Object describing the allowed values of the property. IDictionary <string, OpenApiSchema> properties = new Dictionary <string, OpenApiSchema>(); // structure properties foreach (var property in structuredType.DeclaredStructuralProperties()) { OpenApiSchema propertySchema = context.CreatePropertySchema(property); propertySchema.Description = context.Model.GetDescriptionAnnotation(property); propertySchema.Extensions.AddCustomAtributesToExtensions(context, property); properties.Add(property.Name, propertySchema); } // navigation properties foreach (var property in structuredType.DeclaredNavigationProperties()) { OpenApiSchema propertySchema = context.CreateEdmTypeSchema(property.Type); propertySchema.Description = context.Model.GetDescriptionAnnotation(property); propertySchema.Extensions.AddCustomAtributesToExtensions(context, property); properties.Add(property.Name, propertySchema); } return(properties); }
/// <summary> /// Create a map of string/<see cref="OpenApiSchema"/> map for a <see cref="IEdmStructuredType"/>'s all properties. /// </summary> /// <param name="context">The OData context.</param> /// <param name="structuredType">The Edm structured type.</param> /// <returns>The created map of <see cref="OpenApiSchema"/>.</returns> public static IDictionary <string, OpenApiSchema> CreateStructuredTypeCombinedPropertiesSchema(this ODataContext context, IEdmStructuredType structuredType) { Utils.CheckArgumentNull(context, nameof(context)); Utils.CheckArgumentNull(structuredType, nameof(structuredType)); // The name is the property name, the value is a Schema Object describing the allowed values of the property. IDictionary <string, OpenApiSchema> properties = new Dictionary <string, OpenApiSchema>(); // structure properties foreach (var property in structuredType.StructuralProperties()) { // OpenApiSchema propertySchema = property.Type.CreateSchema(); // propertySchema.Default = property.DefaultValueString != null ? new OpenApiString(property.DefaultValueString) : null; if (property.Type.FullName() != structuredType.FullTypeName() && property.Type.FullName() != $"Collection({structuredType.FullTypeName()})") { properties.Add(property.Name, context.CreatePropertySchema(property)); } } // navigation properties foreach (var property in structuredType.NavigationProperties()) { if (property.Type.FullName() != structuredType.FullTypeName() && property.Type.FullName() != $"Collection({structuredType.FullTypeName()})") { OpenApiSchema propertySchema = context.CreateEdmTypeSchema(property.Type); properties.Add(property.Name, propertySchema); } } return(properties); }
public void CreatePropertySchemaForNullableEnumPropertyReturnSchema() { // Arrange IEdmModel model = EdmModelHelper.BasicEdmModel; ODataContext context = new ODataContext(model); IEdmEnumType enumType = model.SchemaElements.OfType <IEdmEnumType>().First(e => e.Name == "Color"); EdmEntityType entitType = new EdmEntityType("NS", "Entity"); IEdmProperty property = new EdmStructuralProperty(entitType, "ColorEnumValue", new EdmEnumTypeReference(enumType, true), "yellow"); // Act var schema = context.CreatePropertySchema(property); Assert.NotNull(schema); string json = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); _output.WriteLine(json); // Assert Assert.Equal(@"{ ""anyOf"": [ { ""$ref"": ""#/components/schemas/DefaultNs.Color"" } ], ""default"": ""yellow"", ""nullable"": true }".ChangeLineBreaks(), json); }
public void NonNullableBooleanPropertyWithDefaultValueWorks() { // Arrange ODataContext context = new ODataContext(EdmModelHelper.BasicEdmModel); EdmEntityType entitType = new EdmEntityType("NS", "Entity"); IEdmStructuralProperty property = new EdmStructuralProperty( entitType, "BooleanValue", EdmCoreModel.Instance.GetBoolean(false), "false"); // Act var schema = context.CreatePropertySchema(property); // Assert Assert.NotNull(schema); Assert.Equal("boolean", schema.Type); string json = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); Assert.Equal(@"{ ""type"": ""boolean"", ""default"": false }".ChangeLineBreaks(), json); }
public void NonNullableDoublePropertyWithDefaultStringWorks() { // Arrange ODataContext context = new ODataContext(EdmModelHelper.BasicEdmModel); EdmEntityType entitType = new EdmEntityType("NS", "Entity"); IEdmStructuralProperty property = new EdmStructuralProperty( entitType, "DoubleValue", EdmCoreModel.Instance.GetDouble(false), "3.1415926535897931"); // Act var schema = context.CreatePropertySchema(property); // Assert Assert.NotNull(schema); Assert.Null(schema.Type); string json = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); Assert.Equal(@"{ ""anyOf"": [ { ""type"": ""number"" }, { ""type"": ""string"" }, { ""enum"": [ ""-INF"", ""INF"", ""NaN"" ] } ], ""format"": ""double"", ""default"": ""3.1415926535897931"" }".ChangeLineBreaks(), json); }