Skip to content

Commit

Permalink
Port to 7.x: Fix issue #1320 - write metadata async issue (#3084)
Browse files Browse the repository at this point in the history
* Fix issue #1320 by addressing metadata async issues, refactoring to use asynchronous methods, and improving error handling. Added and refactored tests, including those for LargeEdmModel and async methods, ensuring robust handling of `System.InvalidOperationException`. Added missing `.ConfigureAwait(false)` and `Contract.Assert` for better task management.
  • Loading branch information
WanjohiSammy authored Oct 17, 2024
1 parent ce2a3b9 commit 2a559d8
Show file tree
Hide file tree
Showing 8 changed files with 1,012 additions and 174 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.OData.Edm/Csdl/SchemaWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ internal static async Task<Tuple<bool, IEnumerable<EdmError>>> TryWriteSchemaAsy
return Tuple.Create(false, errors);
}

IEnumerable<EdmSchema> schemas = new EdmModelSchemaSeparationSerializationVisitor(model).GetSchemas();
IEnumerable<EdmSchema> schemas = await (new EdmModelSchemaSeparationSerializationVisitor(model)).GetSchemasAsync().ConfigureAwait(false);
if (schemas.Count() > 1 && singleFileExpected)
{
errors = new EdmError[] { new EdmError(new CsdlLocation(0, 0), EdmErrorCode.SingleFileExpected, Edm.Strings.Serializer_SingleFileExpected) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,18 @@ internal async Task VisitEdmSchemaAsync(EdmSchema element, IEnumerable<KeyValueP

await this.schemaWriter.WriteSchemaElementHeaderAsync(element, alias, mappings).ConfigureAwait(false);

VisitSchemaElements(element.SchemaElements);
await VisitSchemaElementsAsync(element.SchemaElements).ConfigureAwait(false);

// process the functions/actions seperately
foreach (KeyValuePair<string, IList<IEdmSchemaElement>> operation in element.SchemaOperations)
{
await this.schemaWriter.WriteSchemaOperationsHeaderAsync(operation).ConfigureAwait(false);
VisitSchemaElements(operation.Value.AsEnumerable<IEdmSchemaElement>()); // Call AsEnumerable() to make .net 3.5 happy
await VisitSchemaElementsAsync(operation.Value.AsEnumerable<IEdmSchemaElement>()).ConfigureAwait(false);
await this.schemaWriter.WriteSchemaOperationsEndAsync(operation).ConfigureAwait(false);
}

// EntityContainers are excluded from the EdmSchema.SchemaElements property so they can be forced to the end.
VisitCollection(element.EntityContainers, async (e) => await this.ProcessEntityContainerAsync(e).ConfigureAwait(false));
await VisitCollectionAsync(element.EntityContainers, this.ProcessEntityContainerAsync).ConfigureAwait(false);

if (element.OutOfLineAnnotations.Any())
{
Expand Down Expand Up @@ -304,8 +304,8 @@ protected override async Task ProcessEntityTypeAsync(IEdmEntityType element)
await this.VisitEntityTypeDeclaredKeyAsync(element.DeclaredKey).ConfigureAwait(false);
}

this.VisitProperties(element.DeclaredStructuralProperties().Cast<IEdmProperty>());
this.VisitProperties(element.DeclaredNavigationProperties().Cast<IEdmProperty>());
await this.VisitPropertiesAsync(element.DeclaredStructuralProperties().Cast<IEdmProperty>()).ConfigureAwait(false);
await this.VisitPropertiesAsync(element.DeclaredNavigationProperties().Cast<IEdmProperty>()).ConfigureAwait(false);
await this.EndElementAsync(element).ConfigureAwait(false);
}

Expand All @@ -331,7 +331,7 @@ protected override async Task ProcessStructuralPropertyAsync(IEdmStructuralPrope
await this.BeginElementAsync(element, (IEdmStructuralProperty t) => this.schemaWriter.WriteStructuralPropertyElementHeaderAsync(t, inlineType), e => this.ProcessFacetsAsync(e.Type, inlineType)).ConfigureAwait(false);
if (!inlineType)
{
VisitTypeReference(element.Type);
await VisitTypeReferenceAsync(element.Type).ConfigureAwait(false);
}

await this.EndElementAsync(element).ConfigureAwait(false);
Expand Down Expand Up @@ -558,7 +558,7 @@ protected override async Task ProcessTermAsync(IEdmTerm term)
{
if (term.Type != null)
{
VisitTypeReference(term.Type);
await VisitTypeReferenceAsync(term.Type).ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -629,7 +629,7 @@ await this.BeginElementAsync(
).ConfigureAwait(false);
if (!inlineType)
{
VisitTypeReference(element.Type);
await VisitTypeReferenceAsync(element.Type).ConfigureAwait(false);
}

await this.VisitPrimitiveElementAnnotationsAsync(this.Model.DirectValueAnnotations(element)).ConfigureAwait(false);
Expand Down Expand Up @@ -691,7 +691,7 @@ await this.BeginElementAsync(
}
else
{
this.VisitTypeReference(type);
await this.VisitTypeReferenceAsync(type).ConfigureAwait(false);
}
}).ConfigureAwait(false);
await this.EndElementAsync(operationReturn).ConfigureAwait(false);
Expand Down Expand Up @@ -726,7 +726,7 @@ await this.BeginElementAsync(
).ConfigureAwait(false);
if (!inlineType)
{
VisitTypeReference(element.ElementType);
await VisitTypeReferenceAsync(element.ElementType).ConfigureAwait(false);
}

await this.EndElementAsync(element).ConfigureAwait(false);
Expand Down Expand Up @@ -1031,7 +1031,7 @@ protected override async Task ProcessIsTypeExpressionAsync(IEdmIsTypeExpression

if (!inlineType)
{
VisitTypeReference(expression.Type);
await VisitTypeReferenceAsync(expression.Type).ConfigureAwait(false);
}

this.VisitExpression(expression.Operand);
Expand Down Expand Up @@ -1279,7 +1279,7 @@ protected override async Task ProcessCastExpressionAsync(IEdmCastExpression expr

if (!inlineType)
{
VisitTypeReference(expression.Type);
await VisitTypeReferenceAsync(expression.Type).ConfigureAwait(false);
}

this.VisitExpression(expression.Operand);
Expand Down Expand Up @@ -1412,7 +1412,7 @@ private async Task ProcessOperationAsync<TOperation>(TOperation operation, Func<
await this.BeginElementAsync(operation, writeElementAction).ConfigureAwait(false);

await this.schemaWriter.WriteOperationParametersBeginAsync(operation.Parameters).ConfigureAwait(false);
this.VisitOperationParameters(operation.Parameters);
await this.VisitOperationParametersAsync(operation.Parameters).ConfigureAwait(false);
await this.schemaWriter.WriteOperationParametersEndAsync(operation.Parameters).ConfigureAwait(false);

IEdmOperationReturn operationReturn = operation.GetReturn();
Expand Down Expand Up @@ -1501,12 +1501,12 @@ private async Task ProcessFacetsAsync(IEdmTypeReference element, bool inlineType
{
IEdmCollectionTypeReference collectionElement = element.AsCollection();
await this.schemaWriter.WriteNullableAttributeAsync(collectionElement.CollectionDefinition().ElementType).ConfigureAwait(false);
VisitTypeReference(collectionElement.CollectionDefinition().ElementType);
await VisitTypeReferenceAsync(collectionElement.CollectionDefinition().ElementType).ConfigureAwait(false);
}
else
{
await this.schemaWriter.WriteNullableAttributeAsync(element).ConfigureAwait(false);
VisitTypeReference(element);
await VisitTypeReferenceAsync(element).ConfigureAwait(false);
}
}
}
Expand Down
Loading

0 comments on commit 2a559d8

Please sign in to comment.