internal override void WriteTypeDefinitionAttributes(IEdmTypeDefinitionReference reference) { IEdmTypeReference actualTypeReference = reference.AsActualTypeReference(); if (actualTypeReference.IsBinary()) { this.WriteBinaryTypeAttributes(actualTypeReference.AsBinary()); } else if (actualTypeReference.IsString()) { this.WriteStringTypeAttributes(actualTypeReference.AsString()); } else if (actualTypeReference.IsTemporal()) { this.WriteTemporalTypeAttributes(actualTypeReference.AsTemporal()); } else if (actualTypeReference.IsDecimal()) { this.WriteDecimalTypeAttributes(actualTypeReference.AsDecimal()); } else if (actualTypeReference.IsSpatial()) { this.WriteSpatialTypeAttributes(actualTypeReference.AsSpatial()); } }
/// <summary> /// Gets the type name based on the given odata value. /// </summary> /// <param name="value">The value.</param> /// <param name="model">The model used to handle unsigned int conversions.</param> /// <returns>The type name for the context URI.</returns> private static string GetTypeNameForValue(ODataValue value, IEdmModel model) { if (value == null) { return(null); } // special identifier for null values. if (value.IsNullValue) { return(ODataConstants.ContextUriFragmentNull); } if (value.TypeAnnotation != null && !string.IsNullOrEmpty(value.TypeAnnotation.TypeName)) { return(value.TypeAnnotation.TypeName); } var collectionValue = value as ODataCollectionValue; if (collectionValue != null) { return(EdmLibraryExtensions.GetCollectionTypeFullName(collectionValue.TypeName)); } var enumValue = value as ODataEnumValue; if (enumValue != null) { return(enumValue.TypeName); } var untypedValue = value as ODataUntypedValue; if (untypedValue != null) { return(ODataConstants.ContextUriFragmentUntyped); } ODataPrimitiveValue primitive = value as ODataPrimitiveValue; if (primitive == null) { Debug.Assert(value is ODataStreamReferenceValue, "value is ODataStreamReferenceValue"); throw new ODataException(Strings.ODataContextUriBuilder_StreamValueMustBePropertiesOfODataResource); } // Try convert to underlying type if the primitive value is unsigned int. IEdmTypeDefinitionReference typeDefinitionReference = model.ResolveUIntTypeDefinition(primitive.Value); if (typeDefinitionReference != null) { return(typeDefinitionReference.FullName()); } IEdmPrimitiveTypeReference primitiveValueTypeReference = EdmLibraryExtensions.GetPrimitiveTypeReference(primitive.Value.GetType()); return(primitiveValueTypeReference == null ? null : primitiveValueTypeReference.FullName()); }
public void TestCapabilitiesVocabularySupportedTerm(string name, string appliesTo) { string qualifiedName = "Org.OData.Capabilities.V1." + name; var supported = this.capVocModel.FindDeclaredTerm(qualifiedName); Assert.NotNull(supported); Assert.Equal("Org.OData.Capabilities.V1", supported.Namespace); Assert.Equal(name, supported.Name); Assert.Equal(appliesTo, supported.AppliesTo); var type = supported.Type; Assert.Equal("Org.OData.Core.V1.Tag", type.FullName()); Assert.Equal(EdmTypeKind.TypeDefinition, type.TypeKind()); IEdmTypeDefinitionReference typeDefinitionReference = type.AsTypeDefinition(); Assert.NotNull(typeDefinitionReference); Assert.Equal(EdmPrimitiveTypeKind.Boolean, typeDefinitionReference.TypeDefinition().UnderlyingType.PrimitiveKind); }
/// <summary> /// If this reference is of a type definition, this will return a valid type definition reference to the type definition. Otherwise, it will return a bad type definition reference. /// </summary> /// <param name="type">Reference to the calling object.</param> /// <returns>A valid type definition reference if the definition of the reference is of a type definition. Otherwise a bad type definition reference.</returns> public static IEdmTypeDefinitionReference AsTypeDefinition(this IEdmTypeReference type) { EdmUtil.CheckArgumentNull(type, "type"); IEdmTypeDefinitionReference reference = type as IEdmTypeDefinitionReference; if (reference != null) { return(reference); } IEdmType typeDefinition = type.Definition; if (typeDefinition.TypeKind == EdmTypeKind.TypeDefinition) { return(new EdmTypeDefinitionReference((IEdmTypeDefinition)typeDefinition, type.IsNullable)); } string typeFullName = type.FullName(); return(new EdmTypeDefinitionReference( new BadTypeDefinition(typeFullName, ConversionError(type.Location(), typeFullName, EdmConstants.Type_TypeDefinition)), type.IsNullable)); }
protected override void ProcessTypeDefinitionReference(IEdmTypeDefinitionReference element) { this.schemaWriter.WriteTypeDefinitionAttributes(element); }
protected override void ProcessTypeDefinitionReference(IEdmTypeDefinitionReference element) { this.CheckSchemaElementReference(element.TypeDefinition()); }
/// <summary> /// Reads the next parameter from the parameters payload. /// </summary> /// <param name="propertyAndAnnotationCollector">The duplicate property names checker used to read a parameter payload.</param> /// <returns>true if a parameter was read from the payload; otherwise false.</returns> /// <remarks> /// Pre-Condition: Property or EndObject the property node of the parameter to read or the end object node if there are not parameters /// Post-Condition: Property or EndObject the node after the property value of a primitive, complex or null collection parameter /// Any the start of the value representing a non-null collection parameter (the collection reader will fail if this is not a StartArray node) /// </remarks> internal bool ReadNextParameter(PropertyAndAnnotationCollector propertyAndAnnotationCollector) { Debug.Assert(propertyAndAnnotationCollector != null, "propertyAndAnnotationCollector != null"); this.AssertJsonCondition(JsonNodeType.Property, JsonNodeType.EndObject); bool parameterRead = false; if (this.JsonReader.NodeType == JsonNodeType.Property) { bool foundCustomInstanceAnnotation = false; this.ProcessProperty( propertyAndAnnotationCollector, propertyAnnotationValueReader, (propertyParsingResult, parameterName) => { if (this.JsonReader.NodeType == JsonNodeType.Property) { // Read over property name this.JsonReader.Read(); } switch (propertyParsingResult) { case PropertyParsingResult.ODataInstanceAnnotation: // OData instance annotations are not supported in parameter payloads. throw new ODataException(ODataErrorStrings.ODataJsonLightPropertyAndValueDeserializer_UnexpectedAnnotationProperties(parameterName)); case PropertyParsingResult.CustomInstanceAnnotation: this.JsonReader.SkipValue(); foundCustomInstanceAnnotation = true; break; case PropertyParsingResult.PropertyWithoutValue: throw new ODataException(ODataErrorStrings.ODataJsonLightParameterDeserializer_PropertyAnnotationWithoutPropertyForParameters(parameterName)); case PropertyParsingResult.EndOfObject: break; case PropertyParsingResult.MetadataReferenceProperty: throw new ODataException(ODataErrorStrings.ODataJsonLightPropertyAndValueDeserializer_UnexpectedMetadataReferenceProperty(parameterName)); case PropertyParsingResult.PropertyWithValue: IEdmTypeReference parameterTypeReference = this.parameterReader.GetParameterTypeReference(parameterName); Debug.Assert(parameterTypeReference != null, "parameterTypeReference != null"); ODataParameterReaderState state; object parameterValue; EdmTypeKind parameterTypeKind = parameterTypeReference.TypeKind(); switch (parameterTypeKind) { case EdmTypeKind.Primitive: IEdmPrimitiveTypeReference primitiveTypeReference = parameterTypeReference.AsPrimitive(); if (primitiveTypeReference.PrimitiveKind() == EdmPrimitiveTypeKind.Stream) { throw new ODataException(ODataErrorStrings.ODataJsonLightParameterDeserializer_UnsupportedPrimitiveParameterType(parameterName, primitiveTypeReference.PrimitiveKind())); } parameterValue = this.ReadNonEntityValue( /*payloadTypeName*/ null, primitiveTypeReference, /*propertyAndAnnotationCollector*/ null, /*collectionValidator*/ null, /*validateNullValue*/ true, /*isTopLevelPropertyValue*/ false, /*insideComplexValue*/ false, parameterName); state = ODataParameterReaderState.Value; break; case EdmTypeKind.Enum: IEdmEnumTypeReference enumTypeReference = parameterTypeReference.AsEnum(); parameterValue = this.ReadNonEntityValue( /*payloadTypeName*/ null, enumTypeReference, /*propertyAndAnnotationCollector*/ null, /*collectionValidator*/ null, /*validateNullValue*/ true, /*isTopLevelPropertyValue*/ false, /*insideComplexValue*/ false, parameterName); state = ODataParameterReaderState.Value; break; case EdmTypeKind.TypeDefinition: IEdmTypeDefinitionReference typeDefinitionReference = parameterTypeReference.AsTypeDefinition(); parameterValue = this.ReadNonEntityValue( /*payloadTypeName*/ null, typeDefinitionReference, /*propertyAndAnnotationCollector*/ null, /*collectionValidator*/ null, /*validateNullValue*/ true, /*isTopLevelPropertyValue*/ false, /*insideComplexValue*/ false, parameterName); state = ODataParameterReaderState.Value; break; case EdmTypeKind.Complex: case EdmTypeKind.Entity: parameterValue = null; state = ODataParameterReaderState.Resource; break; case EdmTypeKind.Collection: parameterValue = null; if (this.JsonReader.NodeType == JsonNodeType.PrimitiveValue) { // NOTE: we support null collections in parameter payloads but nowhere else. parameterValue = this.JsonReader.ReadPrimitiveValue(); if (parameterValue != null) { throw new ODataException(ODataErrorStrings.ODataJsonLightParameterDeserializer_NullCollectionExpected(JsonNodeType.PrimitiveValue, parameterValue)); } state = ODataParameterReaderState.Value; } else if (((IEdmCollectionType)parameterTypeReference.Definition).ElementType.IsStructured()) { state = ODataParameterReaderState.ResourceSet; } else { state = ODataParameterReaderState.Collection; } break; default: throw new ODataException(ODataErrorStrings.ODataJsonLightParameterDeserializer_UnsupportedParameterTypeKind(parameterName, parameterTypeReference.TypeKind())); } parameterRead = true; this.parameterReader.EnterScope(state, parameterName, parameterValue); Debug.Assert( state == ODataParameterReaderState.Collection || state == ODataParameterReaderState.Resource || state == ODataParameterReaderState.ResourceSet || this.JsonReader.NodeType == JsonNodeType.Property || this.JsonReader.NodeType == JsonNodeType.EndObject, "Expected any node for a collection; 'Property' or 'EndObject' if it is a primitive or complex value."); break; default: throw new ODataException(ODataErrorStrings.General_InternalError(InternalErrorCodes.ODataJsonLightParameterDeserializer_ReadNextParameter)); } }); if (foundCustomInstanceAnnotation) { return(this.ReadNextParameter(propertyAndAnnotationCollector)); } } if (!parameterRead && this.JsonReader.NodeType == JsonNodeType.EndObject) { this.JsonReader.ReadEndObject(); this.ReadPayloadEnd(/*isReadingNestedPayload*/ false); // Pop the scope for the previous parameter if there is any if (this.parameterReader.State != ODataParameterReaderState.Start) { this.parameterReader.PopScope(this.parameterReader.State); } // Pop the 'Start' scope and enter the 'Completed' scope this.parameterReader.PopScope(ODataParameterReaderState.Start); this.parameterReader.EnterScope(ODataParameterReaderState.Completed, /*parameterName*/ null, /*parameterValue*/ null); this.AssertJsonCondition(JsonNodeType.EndOfInput); } return(parameterRead); }
protected virtual void ProcessTypeDefinitionReference(IEdmTypeDefinitionReference reference) { this.ProcessTypeReference(reference); }
private static OpenApiSchema CreateTypeDefinitionSchema(this ODataContext context, IEdmTypeDefinitionReference reference) { Debug.Assert(context != null); Debug.Assert(reference != null); OpenApiSchema schema = new OpenApiSchema(); schema.Reference = null; if (reference.IsNullable && context.Settings.OpenApiSpecVersion >= OpenApiSpecVersion.OpenApi3_0) { schema.AnyOf = new List <OpenApiSchema> { new OpenApiSchema { UnresolvedReference = true, Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = reference.Definition.FullTypeName() } }, new OpenApiSchema { Type = "object", Nullable = true } }; } else { schema.Type = null; schema.AnyOf = null; schema.Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = reference.Definition.FullTypeName() }; schema.UnresolvedReference = true; schema.Nullable = reference.IsNullable; } return(schema); }
/// <summary> /// Reads a type definition value. /// </summary> /// <param name="insideJsonObjectValue">true if the reader is positioned on the first property of the value which is a JSON Object /// (or the second property if the first one was odata.type).</param> /// <param name="expectedValueTypeReference">The expected type definition reference of the value, or null if none is available.</param> /// <param name="validateNullValue">true to validate null values; otherwise false.</param> /// <param name="propertyName">The name of the property whose value is being read, if applicable (used for error reporting).</param> /// <returns>The value of the primitive value.</returns> private object ReadTypeDefinitionValue(bool insideJsonObjectValue, IEdmTypeDefinitionReference expectedValueTypeReference, bool validateNullValue, string propertyName) { object result = this.ReadPrimitiveValue(insideJsonObjectValue, expectedValueTypeReference.AsPrimitive(), validateNullValue, propertyName); // Try convert to the expected CLR types from their underlying CLR types. try { return this.Model.GetPrimitiveValueConverter(expectedValueTypeReference).ConvertFromUnderlyingType(result); } catch (OverflowException) { throw new ODataException(ODataErrorStrings.EdmLibraryExtensions_ValueOverflowForUnderlyingType(result, expectedValueTypeReference.FullName())); } }
internal abstract void WriteTypeDefinitionAttributes(IEdmTypeDefinitionReference reference);
private static OpenApiSchema CreateTypeDefinitionSchema(this ODataContext context, IEdmTypeDefinitionReference reference) { Debug.Assert(context != null); Debug.Assert(reference != null); OpenApiSchema schema = new OpenApiSchema(); schema.Nullable = reference.IsNullable; schema.AnyOf = null; schema.Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = reference.Definition.FullTypeName() }; return(schema); }