Exemple #1
0
        public void CreateEnumTypeSchemaThrowArgumentNullEnumType()
        {
            // Arrange
            ODataContext context = new ODataContext(EdmCoreModel.Instance);

            // Act & Assert
            Assert.Throws <ArgumentNullException>("enumType", () => context.CreateEnumTypeSchema(enumType: null));
        }
Exemple #2
0
        public void CreateEnumTypeSchemaThrowArgumentNullContext()
        {
            // Arrange
            ODataContext context = null;

            // Act & Assert
            Assert.Throws <ArgumentNullException>("context", () => context.CreateEnumTypeSchema(enumType: null));
        }
        /// <summary>
        /// Create a <see cref="OpenApiSchema"/> for a <see cref="IEdmTypeReference"/>.
        /// </summary>
        /// <param name="context">The OData context.</param>
        /// <param name="edmTypeReference">The Edm type reference.</param>
        /// <returns>The created <see cref="OpenApiSchema"/>.</returns>
        public static OpenApiSchema CreateEdmTypeSchema(this ODataContext context, IEdmTypeReference edmTypeReference)
        {
            Utils.CheckArgumentNull(context, nameof(context));
            Utils.CheckArgumentNull(edmTypeReference, nameof(edmTypeReference));

            switch (edmTypeReference.TypeKind())
            {
            case EdmTypeKind.Collection:
                // Collection-valued structural and navigation are represented as Schema Objects of type array.
                // The value of the items keyword is a Schema Object specifying the type of the items.
                return(new OpenApiSchema
                {
                    Type = "array",
                    Items = context.CreateEdmTypeSchema(edmTypeReference.AsCollection().ElementType())
                });

            // Complex, enum, entity, entity reference are represented as JSON References to the Schema Object of the complex,
            // enum, entity and entity reference type, either as local references for types directly defined in the CSDL document,
            // or as external references for types defined in referenced CSDL documents.
            case EdmTypeKind.Complex:
            case EdmTypeKind.Entity:
                return(context.CreateStructuredTypeSchema(edmTypeReference.AsStructured()));

            case EdmTypeKind.Enum:
                return(context.CreateEnumTypeSchema(edmTypeReference.AsEnum()));

            // Primitive properties of type Edm.PrimitiveType, Edm.Stream, and any of the Edm.Geo* types are
            // represented as Schema Objects that are JSON References to definitions in the Definitions Object
            case EdmTypeKind.Primitive:
                IEdmPrimitiveTypeReference primitiveTypeReference = (IEdmPrimitiveTypeReference)edmTypeReference;
                return(context.CreateSchema(primitiveTypeReference));

            case EdmTypeKind.TypeDefinition:
                return(context.CreateSchema(((IEdmTypeDefinitionReference)edmTypeReference).TypeDefinition().UnderlyingType));

            case EdmTypeKind.EntityReference:
                return(context.CreateTypeDefinitionSchema(edmTypeReference.AsTypeDefinition()));

            case EdmTypeKind.None:
            default:
                throw Error.NotSupported(String.Format(SRResource.NotSupportedEdmTypeKind, edmTypeReference.TypeKind()));
            }
        }
        internal static OpenApiSchema CreateSchemaTypeSchema(this ODataContext context, IEdmType edmType)
        {
            Debug.Assert(context != null);
            Debug.Assert(edmType != null);

            switch (edmType.TypeKind)
            {
            case EdmTypeKind.Complex:    // complex type
            case EdmTypeKind.Entity:     // entity type
                return(context.CreateStructuredTypeSchema((IEdmStructuredType)edmType, true, true));

            case EdmTypeKind.Enum:     // enum type
                return(context.CreateEnumTypeSchema((IEdmEnumType)edmType));

            case EdmTypeKind.TypeDefinition:     // type definition
                return(context.CreateSchemaTypeDefinitionSchema((IEdmTypeDefinition)edmType));

            case EdmTypeKind.None:
            default:
                throw Error.NotSupported(String.Format(SRResource.NotSupportedEdmTypeKind, edmType.TypeKind));
            }
        }
Exemple #5
0
        public void CreateEnumTypeSchemaReturnCorrectSchema()
        {
            // Arrange
            IEdmModel    model    = EdmModelHelper.BasicEdmModel;
            ODataContext context  = new ODataContext(model);
            IEdmEnumType enumType = model.SchemaElements.OfType <IEdmEnumType>().First(t => t.Name == "Color");

            Assert.NotNull(enumType); // Guard

            // Act
            var schema = context.CreateEnumTypeSchema(enumType);

            // Assert
            Assert.NotNull(schema);
            Assert.Equal("string", schema.Type);
            Assert.Equal("Enum type 'Color' description.", schema.Description);
            Assert.Equal("Color", schema.Title);

            Assert.NotNull(schema.Enum);
            Assert.Equal(2, schema.Enum.Count);
            Assert.Equal(new string[] { "Blue", "White" }, schema.Enum.Select(e => ((OpenApiString)e).Value));

            // Act
            string json = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);

            // Assert
            Assert.NotNull(json);
            Assert.Equal(@"{
  ""title"": ""Color"",
  ""enum"": [
    ""Blue"",
    ""White""
  ],
  ""type"": ""string"",
  ""description"": ""Enum type 'Color' description.""
}".ChangeLineBreaks(), json);
        }