OData / odata.net

ODataLib: Open Data Protocol - .NET Libraries and Frameworks
https://docs.microsoft.com/odata
Other
687 stars 349 forks source link

Fix issue #1320 metadata async issue #3081

Closed WanjohiSammy closed 1 month ago

WanjohiSammy commented 1 month ago

Issues

This pull request fixes #1320.

Description

Briefly describe the changes of this pull request.

Refactors the code to use the asynchronous method VisitCollectionAsync instead of the synchronous VisitCollection. The VisitCollectionAsync method has the second parameter as Func<T, Task> as opposed to VisitCollection which has the second parameter as Action<T>. This change ensures that the method is awaited properly, maintaining the correct order of execution in an asynchronous context.

The ProcessEntityContainerAsync passed as argument to VisitCollectionAsync is an asynchronous method that processes the entity container and at the end WriteEndElementAsync is called to write the end element. Using VisitCollectionAsync ensures that the ProcessEntityContainerAsync method is awaited before the end element is written and thus prevents the exceptions like below:

System.InvalidOperationException
Token EndAttribute in state Element Start Tag would result in an invalid XML document.
   at System.Xml.XmlWellFormedWriter.ThrowInvalidStateTransition(Token token, State currentState)
   at System.Xml.XmlWellFormedWriter.AdvanceStateAsync(Token token)
   at System.Xml.XmlWellFormedWriter.WriteEndAttributeAsync()
   at System.Xml.XmlWellFormedWriter.AdvanceStateAsync(Token token)
   at System.Xml.XmlWellFormedWriter.WriteEndElementAsync()
   at System.Xml.XmlAsyncCheckWriter.WriteEndElementAsync()
   at Microsoft.OData.Edm.Csdl.Serialization.EdmModelCsdlSchemaXmlWriter.WriteEndElementAsync()
System.InvalidOperationException
An asynchronous operation is already in progress.
   at System.Xml.XmlAsyncCheckWriter.CheckAsync()
   at System.Xml.XmlAsyncCheckWriter.WriteEndElementAsync()
   at Microsoft.OData.Edm.Csdl.Serialization.EdmModelCsdlSchemaXmlWriter.WriteEndElementAsync()
   at Microsoft.OData.Edm.Csdl.Serialization.EdmModelCsdlSerializationVisitor.VisitEdmSchemaAsync(EdmSchema element, IEnumerable`1 mappings)
   at Microsoft.OData.Edm.Csdl.CsdlXmlWriter.WriteSchemasAsync()
   at Microsoft.OData.Edm.Csdl.CsdlXmlWriter.WriteODataCsdlAsync()
   at Microsoft.OData.Edm.Csdl.CsdlXmlWriter.WriteCsdlAsync()

Checklist (Uncheck if it is not completed)

Additional work necessary

If documentation update is needed, please add "Docs Needed" label to the issue and provide details about the required document change in the issue.

habbes commented 1 month ago

@WanjohiSammy The linked issues says it's getting the error message "An asynchronous operation is already in progress". And this PR description says it's fixing the error "Synchronous I/O is not allowed". What shows that these two problems are related or this this PR is fixing the linked issue?

WanjohiSammy commented 1 month ago

@WanjohiSammy The linked issues says it's getting the error message "An asynchronous operation is already in progress". And this PR description says it's fixing the error "Synchronous I/O is not allowed". What shows that these two problems are related or this this PR is fixing the linked issue?

@habbes I have updated the PR description to reflect the issue correctly.

gathogojr commented 1 month ago

@WanjohiSammy Given the significant changes made to EdmModelCsdlSerializationVisitor and EdmModelVisitor classes, please ensure/confirm that the tests that you added sufficiently cover all the changes and all execution paths