From 52981d4cebf4831f94dd59968231740e7891c5a3 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 22 Jan 2025 10:52:14 -0500 Subject: [PATCH] fix: a flaky behaviour for format property serialization Signed-off-by: Vincent Biret --- src/Microsoft.OpenApi/Models/OpenApiSchema.cs | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs index d2ffa88ed..dcab7de86 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs @@ -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)); @@ -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); + } + /// /// Serialize 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. @@ -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);