Exemple #1
0
        public void CreateEdmTypeSchemaReturnSchemaForComplexType(bool isNullable)
        {
            // Arrange
            IEdmModel       model   = EdmModelHelper.TripServiceModel;
            IEdmComplexType complex = model.SchemaElements.OfType <IEdmComplexType>().First(c => c.Name == "AirportLocation");

            Assert.NotNull(complex); // guard
            IEdmComplexTypeReference complexTypeReference = new EdmComplexTypeReference(complex, isNullable);
            ODataContext             context = new ODataContext(model);

            // Act
            var schema = context.CreateEdmTypeSchema(complexTypeReference);

            // & Assert
            Assert.NotNull(schema);

            if (isNullable)
            {
                Assert.Null(schema.Reference);
                Assert.NotNull(schema.AnyOf);
                Assert.NotEmpty(schema.AnyOf);
                var anyOf = Assert.Single(schema.AnyOf);
                Assert.NotNull(anyOf.Reference);
                Assert.Equal(ReferenceType.Schema, anyOf.Reference.Type);
                Assert.Equal(complex.FullTypeName(), anyOf.Reference.Id);
            }
            else
            {
                Assert.Null(schema.AnyOf);
                Assert.NotNull(schema.Reference);
                Assert.Equal(ReferenceType.Schema, schema.Reference.Type);
                Assert.Equal(complex.FullTypeName(), schema.Reference.Id);
            }
        }
Exemple #2
0
 public ODataComplexValue CreateODataComplexValue(IEdmComplexType edmType)
 {
     return(new ODataComplexValue()
     {
         TypeName = edmType.FullTypeName()
     });
 }
Exemple #3
0
        /// <summary>
        /// Create the corresponding Authorization object.
        /// </summary>
        /// <param name="record">The input record.</param>
        /// <returns>The created <see cref="Authorization"/> object.</returns>
        public static Authorization CreateAuthorization(IEdmRecordExpression record)
        {
            if (record == null || record.DeclaredType == null)
            {
                return(null);
            }

            IEdmComplexType complexType = record.DeclaredType.Definition as IEdmComplexType;

            if (complexType == null)
            {
                return(null);
            }

            Authorization auth;

            switch (complexType.FullTypeName())
            {
            case AuthorizationConstants.OpenIDConnect:     // OpenIDConnect
                auth = new OpenIDConnect();
                break;

            case AuthorizationConstants.Http:     // Http
                auth = new Http();
                break;

            case AuthorizationConstants.ApiKey:     // ApiKey
                auth = new ApiKey();
                break;

            case AuthorizationConstants.OAuth2ClientCredentials:     // OAuth2ClientCredentials
                auth = new OAuth2ClientCredentials();
                break;

            case AuthorizationConstants.OAuth2Implicit:     // OAuth2Implicit
                auth = new OAuth2Implicit();
                break;

            case AuthorizationConstants.OAuth2Password:     // OAuth2Password
                auth = new OAuth2Password();
                break;

            case AuthorizationConstants.OAuth2AuthCode:     // OAuth2AuthCode
                auth = new OAuth2AuthCode();
                break;

            case AuthorizationConstants.OAuthAuthorization:     // OAuthAuthorization
            default:
                throw new OpenApiException(String.Format(SRResource.AuthorizationRecordTypeNameNotCorrect, complexType.FullTypeName()));
            }

            if (auth != null)
            {
                auth.Initialize(record);
            }

            return(auth);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ComplexCastPathSegment" /> class.
        /// </summary>
        /// <param name="castType">The type of the cast.</param>
        public ComplexCastPathSegment(IEdmComplexType castType)
        {
            if (castType == null)
            {
                throw Error.ArgumentNull("castType");
            }

            CastType = castType;
            CastTypeName = castType.FullTypeName();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ComplexCastPathSegment" /> class.
        /// </summary>
        /// <param name="castType">The type of the cast.</param>
        public ComplexCastPathSegment(IEdmComplexType castType)
        {
            if (castType == null)
            {
                throw Error.ArgumentNull("castType");
            }

            CastType     = castType;
            CastTypeName = castType.FullTypeName();
        }
Exemple #6
0
        /// <summary>
        /// Validates that the <paramref name="payloadComplexType"/> is assignable to the <paramref name="expectedComplexType"/>
        /// and fails if it's not.
        /// </summary>
        /// <param name="expectedComplexType">The expected complex type reference, the base type of the ComplexType expected.</param>
        /// <param name="payloadComplexType">The payload complex type reference to validate.</param>
        internal static void ValidateComplexTypeIsAssignable(IEdmComplexType expectedComplexType, IEdmComplexType payloadComplexType)
        {
            Debug.Assert(expectedComplexType != null, "expectedComplexType != null");
            Debug.Assert(payloadComplexType != null, "payloadComplexType != null");

            // Complex types could be assignable
            if (!EdmLibraryExtensions.IsAssignableFrom(expectedComplexType, payloadComplexType))
            {
                throw new ODataException(Strings.ValidationUtils_IncompatibleType(payloadComplexType.FullTypeName(), expectedComplexType.FullTypeName()));
            }
        }
        public void CreateEdmTypeSchemaReturnSchemaForComplexType(bool isNullable, OpenApiSpecVersion specVersion)
        {
            // Arrange
            IEdmModel       model   = EdmModelHelper.TripServiceModel;
            IEdmComplexType complex = model.SchemaElements.OfType <IEdmComplexType>().First(c => c.Name == "AirportLocation");

            Assert.NotNull(complex); // guard
            IEdmComplexTypeReference complexTypeReference = new EdmComplexTypeReference(complex, isNullable);
            ODataContext             context = new ODataContext(model);

            context.Settings.OpenApiSpecVersion = specVersion;

            // Act
            var schema = context.CreateEdmTypeSchema(complexTypeReference);

            // & Assert
            Assert.NotNull(schema);

            if (specVersion == OpenApiSpecVersion.OpenApi2_0 || isNullable == false)
            {
                Assert.Null(schema.AnyOf);
                Assert.NotNull(schema.Reference);
                Assert.Equal(ReferenceType.Schema, schema.Reference.Type);
                Assert.Equal(complex.FullTypeName(), schema.Reference.Id);
            }
            else
            {
                Assert.Null(schema.Reference);
                Assert.NotNull(schema.AnyOf);
                Assert.NotEmpty(schema.AnyOf);
                Assert.Equal(2, schema.AnyOf.Count);
                var anyOf = schema.AnyOf.FirstOrDefault();
                Assert.NotNull(anyOf.Reference);
                Assert.Equal(ReferenceType.Schema, anyOf.Reference.Type);
                Assert.Equal(complex.FullTypeName(), anyOf.Reference.Id);
            }
        }
Exemple #8
0
        /// <summary>
        /// Read a complex value from the reader.
        /// </summary>
        /// <param name="complexTypeReference">The type reference of the value to read (or null if no type is available).</param>
        /// <param name="payloadTypeName">The name of the type specified in the payload.</param>
        /// <param name="serializationTypeNameAnnotation">The serialization type name for the complex value (possibly null).</param>
        /// <param name="duplicatePropertyNamesChecker">The duplicate property names checker to use (cached), or null if new one should be created.</param>
        /// <returns>The value read from the payload.</returns>
        /// <remarks>
        /// Pre-Condition:   XmlNodeType.Element   - the element to read the value for.
        ///                  XmlNodeType.Attribute - an attribute on the element to read the value for.
        /// Post-Condition:  XmlNodeType.EndElement - the element has been read.
        ///
        /// Note that this method will not read null values, those should be handled by the caller already.
        /// </remarks>
        private ODataComplexValue ReadComplexValue(
            IEdmComplexTypeReference complexTypeReference,
            string payloadTypeName,
            SerializationTypeNameAnnotation serializationTypeNameAnnotation,
            DuplicatePropertyNamesChecker duplicatePropertyNamesChecker)
        {
            this.AssertXmlCondition(XmlNodeType.Element, XmlNodeType.Attribute);

            this.IncreaseRecursionDepth();

            ODataComplexValue complexValue = new ODataComplexValue();
            IEdmComplexType   complexType  = complexTypeReference == null ? null : (IEdmComplexType)complexTypeReference.Definition;

            // If we have a metadata type for the complex value, use that type name
            // otherwise use the type name from the payload (if there was any).
            complexValue.TypeName = complexType == null ? payloadTypeName : complexType.FullTypeName();
            if (serializationTypeNameAnnotation != null)
            {
                complexValue.SetAnnotation(serializationTypeNameAnnotation);
            }

            // Move to the element (so that if we were on an attribute we can test the element for being empty)
            this.XmlReader.MoveToElement();

            if (duplicatePropertyNamesChecker == null)
            {
                duplicatePropertyNamesChecker = this.CreateDuplicatePropertyNamesChecker();
            }
            else
            {
                duplicatePropertyNamesChecker.Clear();
            }

            ReadOnlyEnumerable <ODataProperty> properties = new ReadOnlyEnumerable <ODataProperty>();

            this.ReadPropertiesImplementation(complexType, properties, duplicatePropertyNamesChecker);
            complexValue.Properties = properties;

            this.AssertXmlCondition(true, XmlNodeType.EndElement);
            Debug.Assert(complexValue != null, "The method should never return null since it doesn't handle null values.");

            this.DecreaseRecursionDepth();

            return(complexValue);
        }
Exemple #9
0
        private void VisitComplexType(IEdmComplexType complex)
        {
            string qualifiedName = complex.FullTypeName();

            if (_types.ContainsKey(qualifiedName))
            {
                return; // processed
            }

            MetaComplexType metaComplex = new MetaComplexType();

            metaComplex.QualifiedName = qualifiedName;
            metaComplex.Name          = complex.Name;
            metaComplex.Abstract      = complex.IsAbstract;
            _types[qualifiedName]     = metaComplex;

            VisitProperties(metaComplex, complex.DeclaredProperties);
            VisitNavProperties(metaComplex, complex.DeclaredNavigationProperties());
        }
Exemple #10
0
        /// <summary>
        /// Creat the corresponding example object.
        /// </summary>
        /// <param name="record">The input record.</param>
        /// <returns>The created example object.</returns>
        public static Example CreateExample(IEdmRecordExpression record)
        {
            if (record == null || record.DeclaredType == null)
            {
                return(null);
            }

            IEdmComplexType complexType = record.DeclaredType.Definition as IEdmComplexType;

            if (complexType == null)
            {
                return(null);
            }

            Example example = null;

            switch (complexType.FullTypeName())
            {
            case "Org.OData.Core.V1.ExternalExample":
                example = new ExternalExample();
                break;

            case "Org.OData.Core.V1.InlineExample":
                example = new InlineExample();
                break;

            default:
                break;
            }

            if (example != null)
            {
                example.Init(record);
            }

            return(example);
        }
Exemple #11
0
        /// <summary>
        /// Validates that the <paramref name="payloadComplexType"/> is assignable to the <paramref name="expectedComplexType"/>
        /// and fails if it's not.
        /// </summary>
        /// <param name="expectedComplexType">The expected complex type reference, the base type of the ComplexType expected.</param>
        /// <param name="payloadComplexType">The payload complex type reference to validate.</param>
        internal static void ValidateComplexTypeIsAssignable(IEdmComplexType expectedComplexType, IEdmComplexType payloadComplexType)
        {
            Debug.Assert(expectedComplexType != null, "expectedComplexType != null");
            Debug.Assert(payloadComplexType != null, "payloadComplexType != null");

            // Complex types could be assignable
            if (!EdmLibraryExtensions.IsAssignableFrom(expectedComplexType, payloadComplexType))
            {
                throw new ODataException(Strings.ValidationUtils_IncompatibleType(payloadComplexType.FullTypeName(), expectedComplexType.FullTypeName()));
            }
        }