Skip to content

Commit

Permalink
fix: a flaky behaviour for format property serialization
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
  • Loading branch information
baywet committed Jan 22, 2025
1 parent 9ccdd80 commit 52981d4
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,14 +583,7 @@ internal void WriteAsItemsProperties(IOpenApiWriter writer)
writer.WriteProperty(OpenApiConstants.Type, Type.ToIdentifier());

// format
if (string.IsNullOrEmpty(Format))
{
Format = AllOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ??
AnyOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ??
OneOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format;
}

writer.WriteProperty(OpenApiConstants.Format, Format);
WriteFormatProperty(writer);

// items
writer.WriteOptionalObject(OpenApiConstants.Items, Items, (w, s) => s.SerializeAsV2(w));
Expand Down Expand Up @@ -643,6 +636,19 @@ internal void WriteAsItemsProperties(IOpenApiWriter writer)
writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi2_0);
}

private void WriteFormatProperty(IOpenApiWriter writer)
{
var formatToWrite = Format;
if (string.IsNullOrEmpty(formatToWrite))
{
formatToWrite = AllOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ??
AnyOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ??
OneOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format;
}

writer.WriteProperty(OpenApiConstants.Format, formatToWrite);
}

/// <summary>
/// Serialize <see cref="OpenApiSchema"/> to Open Api v2.0 and handles not marking the provided property
/// as readonly if its included in the provided list of required properties of parent schema.
Expand All @@ -666,14 +672,7 @@ internal virtual void SerializeAsV2(
writer.WriteProperty(OpenApiConstants.Description, Description);

// format
if (string.IsNullOrEmpty(Format))
{
Format = AllOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ??
AnyOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ??
OneOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format;
}

writer.WriteProperty(OpenApiConstants.Format, Format);
WriteFormatProperty(writer);

// title
writer.WriteProperty(OpenApiConstants.Title, Title);
Expand Down

0 comments on commit 52981d4

Please sign in to comment.