Пример #1
0
 private IEdmVocabularyAnnotation WrapVocabularyAnnotation(CsdlAnnotation annotation, CsdlSemanticsSchema schema, IEdmVocabularyAnnotatable targetContext, CsdlSemanticsAnnotations annotationsContext, string qualifier)
 {
     return(EdmUtil.DictionaryGetOrUpdate(
                this.wrappedAnnotations,
                annotation,
                ann => new CsdlSemanticsVocabularyAnnotation(this, schema, targetContext, annotationsContext, ann, qualifier)));
 }
Пример #2
0
        public void ParseCsdlTypeDefinitionWithMembersWorksAsExpected()
        {
            string json = @"""FilterExpressionType"": {
  ""$Kind"": ""TypeDefinition"",
  ""$UnderlyingType"": ""Edm.String"",
  ""@Validation.AllowedValues"": [
      {
        ""Value"": ""SingleValue""
      },
      {
         ""Value"": ""MultiValue""
      }
  ]
}";

            CsdlTypeDefinition typeDefinition = ParseCsdlSchemaElement(json, SchemaJsonParser.ParseCsdlTypeDefinition);

            Assert.NotNull(typeDefinition);

            Assert.Equal("FilterExpressionType", typeDefinition.Name);
            Assert.Equal("Edm.String", typeDefinition.UnderlyingTypeName);

            CsdlAnnotation annotation = Assert.Single(typeDefinition.VocabularyAnnotations);

            Assert.Equal("Validation.AllowedValues", annotation.Term);
            CsdlCollectionExpression collectionExp = Assert.IsType <CsdlCollectionExpression>(annotation.Expression);

            Assert.Equal(2, collectionExp.ElementValues.Count());
        }
Пример #3
0
        public void ParseCsdlTermWithMembersWorksAsExpected()
        {
            string json = @"""ConformanceLevel"": {
  ""$Kind"": ""Term"",
  ""$Type"": ""Capabilities.ConformanceLevelType"",
  ""$Nullable"": true,
  ""$AppliesTo"": [
    ""EntityContainer""
  ],
  ""@Core.Description"": ""The conformance level achieved by this service""
  }";

            CsdlTerm term = ParseCsdlSchemaElement(json, SchemaJsonParser.ParseCsdlTermType);

            Assert.NotNull(term);

            Assert.Equal("ConformanceLevel", term.Name);

            Assert.Equal("EntityContainer", term.AppliesTo);
            Assert.Null(term.DefaultValue);
            Assert.True(term.HasVocabularyAnnotations);

            CsdlNamedTypeReference namedType = Assert.IsType <CsdlNamedTypeReference>(term.Type);

            Assert.Equal("Capabilities.ConformanceLevelType", namedType.FullName);
            Assert.True(namedType.IsNullable);

            CsdlAnnotation annotation = Assert.Single(term.VocabularyAnnotations);

            Assert.Equal("Core.Description", annotation.Term);
            Assert.IsType <CsdlConstantExpression>(annotation.Expression);
        }
Пример #4
0
        public void ParseCsdlEnumTypeWithMembersWorksAsExpected()
        {
            string json = @"""IsolationLevel"": {
  ""$Kind"": ""EnumType"",
  ""$IsFlags"": true,
  ""Snapshot"": 1,
  ""*****@*****.**"": ""All data returned for a request"",
 ""@Core.Description"": ""Supported isolation levels""
        }";

            CsdlEnumType enumType = ParseCsdlSchemaElement(json, SchemaJsonParser.ParseCsdlEnumType);

            Assert.NotNull(enumType);

            Assert.Equal("IsolationLevel", enumType.Name);
            Assert.True(enumType.IsFlags);
            CsdlAnnotation annotation = Assert.Single(enumType.VocabularyAnnotations);

            Assert.Equal("Core.Description", annotation.Term);
            CsdlConstantExpression descripExp = Assert.IsType <CsdlConstantExpression>(annotation.Expression);

            Assert.Equal("Supported isolation levels", descripExp.Value);

            CsdlEnumMember enumMember = Assert.Single(enumType.Members);

            Assert.Equal("Snapshot", enumMember.Name);
            Assert.Equal(1, enumMember.Value.Value);

            annotation = Assert.Single(enumMember.VocabularyAnnotations);
            Assert.Equal("Core.DisplayName", annotation.Term);
            CsdlConstantExpression displayNameExp = Assert.IsType <CsdlConstantExpression>(annotation.Expression);

            Assert.Equal("All data returned for a request", displayNameExp.Value);
        }
 protected CsdlSemanticsVocabularyAnnotation(CsdlSemanticsSchema schema, IEdmVocabularyAnnotatable targetContext, CsdlSemanticsAnnotations annotationsContext, CsdlAnnotation annotation, string qualifier)
     : base(annotation)
 {
     this.schema             = schema;
     this.Annotation         = annotation;
     this.qualifier          = qualifier ?? annotation.Qualifier;
     this.targetContext      = targetContext;
     this.annotationsContext = annotationsContext;
 }
Пример #6
0
        public void ParseCsdlEntityContainerWithMembersWorksAsExpected()
        {
            string json = @"""DemoService"": {
  ""$Kind"": ""EntityContainer"",
  ""Products"": {
     ""$Type"": ""self.Product"",
     ""$Collection"": true
    },
   ""Me"": {
     ""$Type"": ""self.User""
    },
   ""LeaveRequestApproval"": {
    ""$Action"": ""self.Approval""
  },
 ""ProductsByRating"": {
    ""$EntitySet"": ""Products"",
    ""$Function"": ""self.ProductsByRating""
  },
 ""@UI.DisplayName"": ""Supplier Directory""
}";

            CsdlEntityContainer entityContainer = ParseCsdlSchemaElement(json, SchemaJsonParser.ParseCsdlEntityContainer);

            Assert.NotNull(entityContainer);

            Assert.Equal("DemoService", entityContainer.Name);

            CsdlEntitySet entitySet = Assert.Single(entityContainer.EntitySets);

            Assert.Equal("Products", entitySet.Name);

            CsdlSingleton singleton = Assert.Single(entityContainer.Singletons);

            Assert.Equal("Me", singleton.Name);

            Assert.Equal(2, entityContainer.OperationImports.Count());
            CsdlActionImport actionImport = Assert.Single(entityContainer.OperationImports.OfType <CsdlActionImport>());

            Assert.Equal("LeaveRequestApproval", actionImport.Name);

            CsdlFunctionImport functionImport = Assert.Single(entityContainer.OperationImports.OfType <CsdlFunctionImport>());

            Assert.Equal("ProductsByRating", functionImport.Name);

            CsdlAnnotation annotation = Assert.Single(entityContainer.VocabularyAnnotations);

            Assert.Equal("UI.DisplayName", annotation.Term);
            Assert.IsType <CsdlConstantExpression>(annotation.Expression);
        }
        private IEdmVocabularyAnnotation WrapVocabularyAnnotation(CsdlAnnotation annotation, CsdlSemanticsSchema schema, IEdmVocabularyAnnotatable targetContext, CsdlSemanticsAnnotations annotationsContext, string qualifier)
        {
            CsdlSemanticsVocabularyAnnotation result;

            // Guarantee that multiple calls to wrap a given annotation all return the same object.
            if (this.wrappedAnnotations.TryGetValue(annotation, out result))
            {
                return(result);
            }

            result = (CsdlSemanticsVocabularyAnnotation) new CsdlSemanticsValueAnnotation(schema, targetContext, annotationsContext, annotation, qualifier);

            this.wrappedAnnotations[annotation] = result;
            return(result);
        }
Пример #8
0
        public void ParseCsdlAnnotationWorksAsExpected()
        {
            string annotation = "\"@Measures.ISOCurrency\": \"USD\"";

            CsdlAnnotation csdlAnnotation = ParseAnnotation(annotation, out _);

            Assert.NotNull(csdlAnnotation);

            Assert.Equal("Measures.ISOCurrency", csdlAnnotation.Term);
            Assert.Null(csdlAnnotation.Qualifier);
            Assert.NotNull(csdlAnnotation.Expression);
            CsdlConstantExpression constantExp = Assert.IsType <CsdlConstantExpression>(csdlAnnotation.Expression);

            Assert.Equal(EdmValueKind.String, constantExp.ValueKind);
            Assert.Equal("USD", constantExp.Value);
        }
Пример #9
0
        public void ParseCsdlAnnotationWithQualifierWorksAsExpected()
        {
            string annotation = @"""@UI.DisplayName#Tablet"": {
  ""$Path"": ""FirstName""
}";

            CsdlAnnotation csdlAnnotation = ParseAnnotation(annotation, out _);

            Assert.NotNull(csdlAnnotation);

            Assert.Equal("UI.DisplayName", csdlAnnotation.Term);
            Assert.Equal("Tablet", csdlAnnotation.Qualifier);
            Assert.NotNull(csdlAnnotation.Expression);
            CsdlPathExpression pathExp = Assert.IsType <CsdlPathExpression>(csdlAnnotation.Expression);

            Assert.Equal("FirstName", pathExp.Path);
        }
Пример #10
0
        /// <summary>
        /// Try to parse the input json value <see cref="JsonElement"/> to <see cref="CsdlAnnotation"/>.
        /// </summary>
        /// <param name="annotionName">The input annotation name, annotation name should start with '@'.</param>
        /// <param name="element">The JSON value to parse.</param>
        /// <param name="context">The parser context.</param>
        /// <param name="csdlAnnotation">The built CSDL annotation.</param>
        /// <returns>true/false.</returns>
        public static bool TryParseCsdlAnnotation(string annotionName, JsonElement element, JsonParserContext context, out CsdlAnnotation csdlAnnotation)
        {
            EdmUtil.CheckArgumentNull(context, nameof(context));

            csdlAnnotation = null;

            // An annotation is represented as a member whose name consists of an at (@) character,
            // followed by the qualified name of a term, optionally followed by a hash (#) and a qualifier.
            if (!ParseAnnotationName(annotionName, out string termName, out string qualifier))
            {
                return(false);
            }

            context.EnterScope(annotionName);

            CsdlExpressionBase expression = ParseExpression(element, context);

            context.LeaveScope(annotionName);

            csdlAnnotation = new CsdlAnnotation(termName, qualifier, expression, context.Location());
            return(true);
        }
Пример #11
0
        /// <summary>
        /// Checks whether the specified annoation should be included in the model based
        /// on whether it matches any of the provided <see cref="IEdmIncludeAnnotations"/> references.
        /// </summary>
        /// <param name="annotation">The annotation to test</param>
        /// <param name="target">The target of the annotation, with the alias already resolved if any</param>
        /// <param name="includeAnnotationsIndex">Cache of the schema's out-of-line <see cref="IEdmIncludeAnnotations"/>s indexed by the term namespace.
        /// The annotation should be included if it matches at least one of references in this cache.</param>
        /// <returns>Whether or not the annotation should be included in the model.</returns>
        private bool ShouldIncludeAnnotation(
            CsdlAnnotation annotation,
            string target,
            Dictionary <string, List <CsdlIncludeAnnotations> > includeAnnotationsIndex)
        {
            // include annotation if it matches one of the edmx:IncludeAnnotation references
            // we check if the namespace matches, and (if applicable) also the target namespace and the qualifier
            // see: http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/odata-csdl-xml-v4.01.html#sec_IncludedAnnotations

            string qualifiedTerm = this.ReplaceAlias(annotation.Term);

            EdmUtil.TryGetNamespaceNameFromQualifiedName(target, out string targetNamespace, out _);
            if (EdmUtil.TryGetNamespaceNameFromQualifiedName(qualifiedTerm, out string termNamespace, out _))
            {
                if (includeAnnotationsIndex.TryGetValue(termNamespace, out List <CsdlIncludeAnnotations> includeAnnotations))
                {
                    return(includeAnnotations.Any(include =>
                                                  (string.IsNullOrEmpty(include.Qualifier) || annotation.Qualifier == include.Qualifier) &&
                                                  (string.IsNullOrEmpty(include.TargetNamespace) || targetNamespace == include.TargetNamespace)));
                }
            }

            return(false);
        }
Пример #12
0
        public void ParseCsdlAnnotationWorksButWithErrors()
        {
            string annotation = @"""@UI.DisplayName#Tablet"": {
  ""$Path"": [""FirstName""]
}";

            JsonParserContext context;
            CsdlAnnotation    csdlAnnotation = ParseAnnotation(annotation, out context);

            Assert.NotNull(csdlAnnotation);

            Assert.Equal("UI.DisplayName", csdlAnnotation.Term);
            Assert.Equal("Tablet", csdlAnnotation.Qualifier);
            Assert.NotNull(csdlAnnotation.Expression);
            CsdlPathExpression pathExp = Assert.IsType <CsdlPathExpression>(csdlAnnotation.Expression);

            Assert.Null(pathExp.Path);

            EdmError error = Assert.Single(context.Errors);

            Assert.Equal(EdmErrorCode.UnexpectedValueKind, error.ErrorCode);
            Assert.Equal("[email protected]#Tablet.$Path", error.ErrorLocation.ToString());
            Assert.Equal("An unexpected 'Array' value kind was found when parsing the JSON path '[email protected]#Tablet.$Path'. A 'String' value kind was expected.", error.ErrorMessage);
        }
 public CsdlSemanticsValueAnnotation(CsdlSemanticsSchema schema, IEdmVocabularyAnnotatable targetContext, CsdlSemanticsAnnotations annotationsContext, CsdlAnnotation annotation, string externalQualifier)
     : base(schema, targetContext, annotationsContext, annotation, externalQualifier)
 {
 }
Пример #14
0
        public void ParseCsdlOutOfLineAnnotationWithMembersWorksAsExpected()
        {
            string json = @"""$Annotations"": {
   ""self.Person"": {
       ""@Core.Description#Tablet"": ""Dummy"",
       ""@UI.DisplayName#MyTablet"": {
           ""$Path"": ""FirstName""
       }
   },
   ""self.MyEntityType"": {
      ""@self.Dummy"": true,
      ""@UI.Threshold#Example73"": {
         ""$Type"": ""Edm.Decimal"",
         ""$Cast"": {
           ""$Path"": ""Average""
           }
      }
   }
}";

            IList <CsdlAnnotations> annotations = ParseCsdlSchemaElement(json, SchemaJsonParser.ParseCsdlOutOfLineAnnotations);

            Assert.NotNull(annotations);
            Assert.Equal(2, annotations.Count);

            // #1
            CsdlAnnotations csdlAnnotations = annotations.First(c => c.Target == "self.Person");

            Assert.Null(csdlAnnotations.Qualifier);
            Assert.Equal(2, csdlAnnotations.Annotations.Count());

            // #1.1
            CsdlAnnotation csdlAnnotation = csdlAnnotations.Annotations.First(c => c.Term == "Core.Description");

            Assert.Equal("Tablet", csdlAnnotation.Qualifier);
            CsdlConstantExpression constExp = Assert.IsType <CsdlConstantExpression>(csdlAnnotation.Expression);

            Assert.Equal("Dummy", constExp.Value);
            Assert.Equal(EdmValueKind.String, constExp.ValueKind);

            // #1.2
            csdlAnnotation = csdlAnnotations.Annotations.First(c => c.Term == "UI.DisplayName");
            Assert.Equal("MyTablet", csdlAnnotation.Qualifier);
            CsdlPathExpression pathExp = Assert.IsType <CsdlPathExpression>(csdlAnnotation.Expression);

            Assert.Equal("FirstName", pathExp.Path);

            // #2
            csdlAnnotations = annotations.First(c => c.Target == "self.MyEntityType");
            Assert.Null(csdlAnnotations.Qualifier);
            Assert.Equal(2, csdlAnnotations.Annotations.Count());

            // #2.1
            csdlAnnotation = csdlAnnotations.Annotations.First(c => c.Term == "self.Dummy");
            Assert.Null(csdlAnnotation.Qualifier);
            constExp = Assert.IsType <CsdlConstantExpression>(csdlAnnotation.Expression);
            Assert.Equal("true", constExp.Value);
            Assert.Equal(EdmValueKind.Boolean, constExp.ValueKind);

            // #2.2
            csdlAnnotation = csdlAnnotations.Annotations.First(c => c.Term == "UI.Threshold");
            Assert.Equal("Example73", csdlAnnotation.Qualifier);
            CsdlCastExpression castExp = Assert.IsType <CsdlCastExpression>(csdlAnnotation.Expression);

            Assert.IsType <CsdlDecimalTypeReference>(castExp.Type);

            pathExp = Assert.IsType <CsdlPathExpression>(castExp.Operand);
            Assert.Equal("Average", pathExp.Path);
        }