private static void SetSwaggerType(Parameter parameter, IHttpRouteConstraint routeConstraint) { if (routeConstraint is BoolRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(bool))); } else if (routeConstraint is DateTimeRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(DateTime))); } else if (routeConstraint is DecimalRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(decimal))); } else if (routeConstraint is DoubleRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(double))); } else if (routeConstraint is FloatRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(float))); } else if (routeConstraint is GuidRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(Guid))); } else if (routeConstraint is IntRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(int))); } else if (routeConstraint is LongRouteConstraint) { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(long))); } else { SetSwaggerType(parameter, EdmLibHelpers.GetEdmPrimitiveTypeOrNull(typeof(string))); } }
private IEdmProperty CreateStructuralTypeCollectionPropertyBody(EdmStructuredType type, CollectionPropertyConfiguration collectionProperty) { IEdmTypeReference elementTypeReference = null; Type clrType = TypeHelper.GetUnderlyingTypeOrSelf(collectionProperty.ElementType); if (clrType.GetTypeInfo().IsEnum) { IEdmType edmType = GetEdmType(clrType); if (edmType == null) { throw Error.InvalidOperation(SRResources.EnumTypeDoesNotExist, clrType.Name); } IEdmEnumType enumElementType = (IEdmEnumType)edmType; bool isNullable = collectionProperty.ElementType != clrType; elementTypeReference = new EdmEnumTypeReference(enumElementType, isNullable); } else { IEdmType edmType = GetEdmType(collectionProperty.ElementType); if (edmType != null) { IEdmComplexType elementType = edmType as IEdmComplexType; Contract.Assert(elementType != null); elementTypeReference = new EdmComplexTypeReference(elementType, collectionProperty.OptionalProperty); } else { elementTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType); Contract.Assert(elementTypeReference != null); } } return(type.AddStructuralProperty( collectionProperty.Name, new EdmCollectionTypeReference(new EdmCollectionType(elementTypeReference)))); }
// Generates the OrderByQueryOption to use by default for $skip or $top // when no other $orderby is available. It will produce a stable sort. // This may return a null if there are no available properties. private static OrderByQueryOption GenerateDefaultOrderBy(ODataQueryContext context) { string orderByRaw = String.Empty; if (EdmLibHelpers.IsDynamicTypeWrapper(context.ElementClrType)) { orderByRaw = String.Join(",", context.ElementClrType.GetTypeInfo().GetProperties() .Where(property => EdmLibHelpers.GetEdmPrimitiveTypeOrNull(property.PropertyType) != null) .Select(property => property.Name)); } else { orderByRaw = String.Join(",", GetAvailableOrderByProperties(context) .Select(property => property.Name)); } return(String.IsNullOrEmpty(orderByRaw) ? null : new OrderByQueryOption(orderByRaw, context)); }
private string GetAutoExpandRawValue() { var expandRawValue = RawValues.Expand; IEdmEntityType baseEntityType = Context.TargetStructuredType as IEdmEntityType; var autoExpandRawValue = String.Empty; var autoExpandNavigationProperties = EdmLibHelpers.GetAutoExpandNavigationProperties( Context.TargetProperty, Context.TargetStructuredType, Context.Model, !String.IsNullOrEmpty(RawValues.Select)); foreach (var property in autoExpandNavigationProperties) { if (!String.IsNullOrEmpty(autoExpandRawValue)) { autoExpandRawValue += ","; } if (property.DeclaringEntityType() != baseEntityType) { autoExpandRawValue += String.Format(CultureInfo.InvariantCulture, "{0}/", property.DeclaringEntityType().FullTypeName()); } autoExpandRawValue += property.Name; } if (!String.IsNullOrEmpty(autoExpandRawValue)) { if (!String.IsNullOrEmpty(expandRawValue)) { expandRawValue = String.Format(CultureInfo.InvariantCulture, "{0},{1}", autoExpandRawValue, expandRawValue); } else { expandRawValue = autoExpandRawValue; } } return(expandRawValue); }
private ParameterExpression HandleLambdaParameters(IEnumerable <RangeVariable> rangeVariables) { ParameterExpression lambdaIt = null; EnterLambdaScope(); Dictionary <string, ParameterExpression> newParameters = new Dictionary <string, ParameterExpression>(); foreach (RangeVariable rangeVariable in rangeVariables) { ParameterExpression parameter; // Create a Parameter Expression for rangeVariables which are not $it Lambda parameters or $this. if (!_lambdaParameters.TryGetValue(rangeVariable.Name, out parameter) && rangeVariable.Name != ODataThisParameterName) { // Work-around issue 481323 where UriParser yields a collection parameter type // for primitive collections rather than the inner element type of the collection. // Remove this block of code when 481323 is resolved. IEdmTypeReference edmTypeReference = rangeVariable.TypeReference; IEdmCollectionTypeReference collectionTypeReference = edmTypeReference as IEdmCollectionTypeReference; if (collectionTypeReference != null) { IEdmCollectionType collectionType = collectionTypeReference.Definition as IEdmCollectionType; if (collectionType != null) { edmTypeReference = collectionType.ElementType; } } parameter = Expression.Parameter(EdmLibHelpers.GetClrType(edmTypeReference, Model, InternalAssembliesResolver), rangeVariable.Name); Contract.Assert(lambdaIt == null, "There can be only one parameter in an Any/All lambda"); lambdaIt = parameter; } newParameters.Add(rangeVariable.Name, parameter); } _lambdaParameters = newParameters; return(lambdaIt); }
public static bool IsAutoExpand(IEdmProperty navigationProperty, IEdmProperty pathProperty, IEdmStructuredType pathStructuredType, IEdmModel edmModel, bool isSelectPresent = false, ModelBoundQuerySettings querySettings = null) { QueryableRestrictionsAnnotation annotation = EdmLibHelpers.GetPropertyRestrictions(navigationProperty, edmModel); if (annotation != null && annotation.Restrictions.AutoExpand) { return(!annotation.Restrictions.DisableAutoExpandWhenSelectIsPresent || !isSelectPresent); } if (querySettings == null) { querySettings = EdmLibHelpers.GetModelBoundQuerySettings(pathProperty, pathStructuredType, edmModel); } if (querySettings != null && querySettings.IsAutomaticExpand(navigationProperty.Name)) { return(true); } return(false); }
private Expression CreatePropertyAccessExpression(Expression source, IEdmProperty property) { string propertyName = EdmLibHelpers.GetClrPropertyName(property, Model); if (QuerySettings.HandleNullPropagation == HandleNullPropagationOption.True && IsNullable(source.Type) && source != this._lambdaParameter) { Expression propertyAccessExpression = Expression.Property(RemoveInnerNullPropagation(source), propertyName); // source.property => source == null ? null : [CastToNullable]RemoveInnerNullPropagation(source).property // Notice that we are checking if source is null already. so we can safely remove any null checks when doing source.Property Expression ifFalse = ToNullable(ConvertNonStandardPrimitives(propertyAccessExpression)); return (Expression.Condition( test: Expression.Equal(source, NullConstant), ifTrue: Expression.Constant(null, ifFalse.Type), ifFalse: ifFalse)); } else { return(ConvertNonStandardPrimitives(Expression.Property(source, propertyName))); } }
private Type RetrieveClrTypeForConstant(IEdmTypeReference edmTypeReference, ref object value) { Type constantType = EdmLibHelpers.GetClrType(edmTypeReference, Model, InternalAssembliesResolver); if (value != null && edmTypeReference != null && edmTypeReference.IsEnum()) { ODataEnumValue odataEnumValue = (ODataEnumValue)value; string strValue = odataEnumValue.Value; Contract.Assert(strValue != null); constantType = Nullable.GetUnderlyingType(constantType) ?? constantType; value = Enum.Parse(constantType, strValue); } if (edmTypeReference != null && edmTypeReference.IsNullable && (edmTypeReference.IsDate() || edmTypeReference.IsTimeOfDay())) { constantType = Nullable.GetUnderlyingType(constantType) ?? constantType; } return(constantType); }
public override object ReadInline(ODataCollectionValue collection, ODataDeserializerContext readContext) { if (readContext == null) { throw Error.ArgumentNull("readContext"); } if (collection == null) { return(null); } RecurseEnter(readContext); IEdmTypeReference elementType = _edmCollectionType.ElementType(); ODataEntryDeserializer deserializer = DeserializerProvider.GetODataDeserializer(elementType); Contract.Assert(deserializer != null); IList collectionValue = CreateNewCollection(EdmLibHelpers.GetClrType(elementType, EdmModel)); foreach (object entry in collection.Items) { if (elementType.IsPrimitive()) { collectionValue.Add(entry); } else { collectionValue.Add(deserializer.ReadInline(entry, readContext)); } } RecurseLeave(readContext); return(collectionValue); }
internal static void ApplyProperty(ODataProperty property, IEdmStructuredTypeReference resourceType, object resource, ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext) { IEdmProperty edmProperty = resourceType.FindProperty(property.Name); string propertyName = property.Name; if (edmProperty != null) { propertyName = EdmLibHelpers.GetClrPropertyName(edmProperty, readContext.Model); } IEdmTypeReference propertyType = edmProperty != null ? edmProperty.Type : null; // open properties have null values EdmTypeKind propertyKind; object value = ConvertValue(property.Value, ref propertyType, deserializerProvider, readContext, out propertyKind); if (propertyKind == EdmTypeKind.Collection) { SetCollectionProperty(resource, edmProperty, value, propertyName); } else { if (!readContext.IsUntyped) { if (propertyKind == EdmTypeKind.Primitive) { value = EdmPrimitiveHelpers.ConvertPrimitiveValue(value, GetPropertyType(resource, propertyName)); } else if (propertyKind == EdmTypeKind.Enum) { value = EnumDeserializationHelpers.ConvertEnumValue(value, GetPropertyType(resource, propertyName)); } } SetProperty(resource, propertyName, value); } }
private static IDictionary <string, string> GetQueryParameters(HttpRequestMessage request, ODataQueryContext context) { IDictionary <string, string> parameters = request.GetQueryNameValuePairs().ToDictionary(p => p.Key, p => p.Value); IEdmEntityType entityType = context.ElementType as IEdmEntityType; if (entityType != null && parameters.ContainsKey("$select")) { var navigationProperties = entityType.NavigationProperties(); if (navigationProperties != null) { string autoExpandNavigationProperties = String.Empty; foreach (var navigationProperty in navigationProperties) { if (EdmLibHelpers.IsAutoExpand(navigationProperty, context.Model)) { if (!String.IsNullOrEmpty(autoExpandNavigationProperties)) { autoExpandNavigationProperties += ","; } autoExpandNavigationProperties += navigationProperty.Name; } } if (!String.IsNullOrEmpty(autoExpandNavigationProperties)) { if (parameters.ContainsKey("$expand")) { parameters["$expand"] += "," + autoExpandNavigationProperties; } else { parameters["$expand"] = autoExpandNavigationProperties; } } } } return(parameters); }
/// <summary> /// Creates a new instance of the backing CLR object for <see cref="ODataEntityDeserializer.EntityType"/>. /// </summary> /// <param name="readContext">The deserializer context.</param> /// <returns>The created CLR object.</returns> public virtual object CreateEntityResource(ODataDeserializerContext readContext) { if (readContext == null) { throw Error.ArgumentNull("readContext"); } IEdmModel model = readContext.Model; if (model == null) { throw Error.Argument("readContext", SRResources.ModelMissingFromReadContext); } if (readContext.IsUntyped) { return(new EdmEntityObject(EntityType)); } else { Type clrType = EdmLibHelpers.GetClrType(EntityType, model); if (clrType == null) { throw new ODataException( Error.Format(SRResources.MappingDoesNotContainEntityType, EntityType.FullName())); } if (readContext.IsDeltaOfT) { return(Activator.CreateInstance(readContext.ResourceType, clrType)); } else { return(Activator.CreateInstance(clrType)); } } }