Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for direct value annotations when reading / writing CSDL model as JSON #3111

Open
akorygin opened this issue Nov 7, 2024 · 1 comment
Assignees

Comments

@akorygin
Copy link

akorygin commented Nov 7, 2024

Direct value annotations are missing after parsing a CSDL model from JSON.

Assemblies affected

Which assemblies and versions are known to be affected e.g. OData .Net lib 8.1.0.0

Reproduce steps

The simplest set of steps to reproduce the issue. If possible, reference a commit that demonstrates the issue.

The following code demonstrates the issue. Even though direct annotations are written into the output JSON document, they are
ignored when reading it back and reported as "Unexpected element".

Note that a similar test that uses XML reader / writer correctly imports direct value annotations and sets them on the corresponding schema elements.

 var modelIn = new EdmModel();
 var type = new EdmComplexType("MyNamespace", "MyType");
 var property = type.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);

 modelIn.DirectValueAnnotationsManager.SetAnnotationValue(
     property,
     "MyNamespace",
     "ColumnSize",
     new EdmIntegerConstant(EdmCoreModel.Instance.GetInt16(false), 8));

 modelIn.AddElement(type);

 var memoryStream = new MemoryStream();
 using var writer = new Utf8JsonWriter(memoryStream);
 Assert.IsTrue(CsdlWriter.TryWriteCsdl(modelIn, writer, out IEnumerable<EdmError> errors));
 Assert.IsNotNull(errors);
 Assert.IsFalse(errors.Any());

 var json = Encoding.UTF8.GetString(memoryStream.ToArray());
 StringAssert.Contains(json, "ColumnSize");

 var reader = new Utf8JsonReader(memoryStream.ToArray());
 Assert.IsTrue(CsdlReader.TryParse(ref reader, new CsdlJsonReaderSettings(), out IEdmModel modelOut, out errors));

 Assert.IsNotNull(modelOut);
 Assert.AreEqual(1, modelOut.SchemaElements.Count());
 // This fails because of "Unexpected element 'ColumnSize' error"
 Assert.IsFalse(errors.Any());

Expected result

When reading CSDL model from JSON we would expect direct value annotations to be imported the same way they are when de-serializing from XML.

What would happen if there wasn't a bug.

Actual result

What is actually happening.

Additional detail

Optional, details of the root cause if known. Delete this section if you have no additional details to add.

@xuzhg xuzhg self-assigned this Nov 12, 2024
@xuzhg
Copy link
Member

xuzhg commented Nov 13, 2024

Any reason why you don't use the vocabulary annotation? That's the OData v4 recommendation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants