public void ParseIncludeWorksAsExpected() { string json = @" { ""$Namespace"": ""org.example.display"", ""$Alias"": ""UI"" }"; using (JsonDocument document = JsonDocument.Parse(json)) { JsonElement rootElement = document.RootElement; JsonParserContext context = new JsonParserContext(); CsdlInclude include = CsdlJsonParser.ParseInclude(rootElement, context); Assert.NotNull(include); Assert.Equal("UI", include.Alias); Assert.Equal("org.example.display", include.Namespace); } }
public void ParseIncludeReportUnExpectedMember() { string json = @" { ""$Namespace"": ""org.example.display"", ""$Unknown"": ""UI"" }"; using (JsonDocument document = JsonDocument.Parse(json)) { JsonElement rootElement = document.RootElement; JsonParserContext context = new JsonParserContext(); CsdlInclude include = CsdlJsonParser.ParseInclude(rootElement, context); Assert.NotNull(include); Assert.Equal("org.example.display", include.Namespace); var error = Assert.Single(context.Errors); Assert.Equal(EdmErrorCode.UnexpectedElement, error.ErrorCode); Assert.Equal("$.$Unknown", error.ErrorLocation.ToString()); Assert.Equal("A member '$.$Unknown' with value type 'String' is unexpected.", error.ErrorMessage); } }
/// <summary> /// Parse the <see cref="JsonElement"/> to a <see cref="IEdmInclude"/>. /// </summary> /// <param name="element">The input JSON element.</param> /// <param name="context">The parser context.</param> /// <returns>null or parsed <see cref="IEdmInclude"/>.</returns> internal static CsdlInclude ParseInclude(JsonElement element, JsonParserContext context) { // Each item in $Include is an object. if (!element.ValidateValueKind(JsonValueKind.Object, context)) { return(null); } string includeNamespace = null; string includeAlias = null; IList <CsdlAnnotation> annotations = new List <CsdlAnnotation>(); element.ParseAsObject(context, (propertyName, propertyValue) => { // Array items are objects that MUST contain the member $Namespace and MAY contain the member $Alias. switch (propertyName) { case "$Alias": // The value of $Alias is a string containing the alias for the included schema. includeAlias = propertyValue.ParseAsString(context); break; case "$Namespace": // The value of $Namespace is a string containing the namespace of the included schema includeNamespace = propertyValue.ParseAsString(context); break; default: // The item objects MAY contain annotations. SchemaJsonParser.ParseCsdlAnnotation(propertyName, propertyValue, context, annotations); break; } }); CsdlInclude include = new CsdlInclude(includeAlias, includeNamespace, context.Location()); annotations.ForEach(a => include.AddAnnotation(a)); return(include); }
public CsdlSemanticsInclude(CsdlSemanticsModel model, CsdlInclude include) : base(include) { this.model = model; this.include = include; }