Skip to content

Commit 8b91278

Browse files
committed
fix: use settings for terse output in serialization extension methods
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
1 parent 9b11f2e commit 8b91278

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,17 @@ private static async Task WriteOpenApiAsync(HidiOptions options, string openApiF
192192
using var outputStream = options.Output.Create();
193193
using var textWriter = new StreamWriter(outputStream);
194194

195-
var settings = new OpenApiWriterSettings
195+
var settings = new OpenApiJsonWriterSettings
196196
{
197197
InlineLocalReferences = options.InlineLocal,
198-
InlineExternalReferences = options.InlineExternal
198+
InlineExternalReferences = options.InlineExternal,
199+
Terse = options.TerseOutput
199200
};
200201
#pragma warning disable CA1308
201202
IOpenApiWriter writer = openApiFormat.ToLowerInvariant() switch
202203
#pragma warning restore CA1308
203204
{
204-
OpenApiConstants.Json => options.TerseOutput ? new(textWriter, settings, options.TerseOutput) : new OpenApiJsonWriter(textWriter, settings, false),
205+
OpenApiConstants.Json => new OpenApiJsonWriter(textWriter, settings),
205206
OpenApiConstants.Yaml => new OpenApiYamlWriter(textWriter, settings),
206207
_ => throw new ArgumentException("Unknown format"),
207208
};

src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ public static Task SerializeAsync<T>(
8888

8989
IOpenApiWriter writer = format.ToLowerInvariant() switch
9090
{
91-
OpenApiConstants.Json => new OpenApiJsonWriter(streamWriter, settings, false),
91+
OpenApiConstants.Json when settings is OpenApiJsonWriterSettings jsonSettings => new OpenApiJsonWriter(streamWriter, jsonSettings),
92+
OpenApiConstants.Json => new OpenApiJsonWriter(streamWriter, settings),
9293
OpenApiConstants.Yaml => new OpenApiYamlWriter(streamWriter, settings),
9394
_ => throw new OpenApiException(string.Format(SRResource.OpenApiFormatNotSupported, format)),
9495
};

src/Microsoft.OpenApi/Writers/OpenApiJsonWriter.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
5+
using System.ComponentModel;
46
using System.IO;
57

68
namespace Microsoft.OpenApi
@@ -14,7 +16,17 @@ public class OpenApiJsonWriter : OpenApiWriterBase
1416
/// Initializes a new instance of the <see cref="OpenApiJsonWriter"/> class.
1517
/// </summary>
1618
/// <param name="textWriter">The text writer.</param>
17-
public OpenApiJsonWriter(TextWriter textWriter) : base(textWriter, null)
19+
public OpenApiJsonWriter(TextWriter textWriter) : this(textWriter, (OpenApiWriterSettings?)null)
20+
{
21+
// this constructor is kept for binary compatibility
22+
// TODO remove in next major version and make the settings an optional parameter in the other constructor
23+
}
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="OpenApiJsonWriter"/> class.
26+
/// </summary>
27+
/// <param name="settings">Settings for controlling how the OpenAPI document will be written out.</param>
28+
/// <param name="textWriter">The text writer.</param>
29+
public OpenApiJsonWriter(TextWriter textWriter, OpenApiWriterSettings? settings) : base(textWriter, settings ?? new OpenApiJsonWriterSettings())
1830
{
1931
}
2032

@@ -34,9 +46,13 @@ public OpenApiJsonWriter(TextWriter textWriter, OpenApiJsonWriterSettings settin
3446
/// <param name="textWriter">The text writer.</param>
3547
/// <param name="settings">Settings for controlling how the OpenAPI document will be written out.</param>
3648
/// <param name="terseOutput"> Setting for allowing the JSON emitted to be in terse format.</param>
37-
public OpenApiJsonWriter(TextWriter textWriter, OpenApiWriterSettings? settings, bool terseOutput = false) : base(textWriter, settings)
49+
[Obsolete("Use OpenApiJsonWriter(TextWriter textWriter, OpenApiJsonWriterSettings settings) instead.")]
50+
[EditorBrowsable(EditorBrowsableState.Never)]
51+
public OpenApiJsonWriter(TextWriter textWriter, OpenApiWriterSettings? settings, bool terseOutput) : base(textWriter, settings)
3852
{
3953
_produceTerseOutput = terseOutput;
54+
// this constructor is kept for binary compatibility, terse information should be read from the settings to avoid fork APIs.
55+
// TODO remove in next major version
4056
}
4157

4258
/// <summary>

src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ protected OpenApiWriterBase(TextWriter textWriter, OpenApiWriterSettings? settin
5555
Writer.NewLine = "\n";
5656

5757
Scopes = new();
58-
if (settings == null)
59-
{
60-
settings = new();
61-
}
58+
settings ??= new();
6259
Settings = settings;
6360
}
6461

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,10 @@ namespace Microsoft.OpenApi
795795
{
796796
public OpenApiJsonWriter(System.IO.TextWriter textWriter) { }
797797
public OpenApiJsonWriter(System.IO.TextWriter textWriter, Microsoft.OpenApi.OpenApiJsonWriterSettings settings) { }
798-
public OpenApiJsonWriter(System.IO.TextWriter textWriter, Microsoft.OpenApi.OpenApiWriterSettings? settings, bool terseOutput = false) { }
798+
public OpenApiJsonWriter(System.IO.TextWriter textWriter, Microsoft.OpenApi.OpenApiWriterSettings? settings) { }
799+
[System.Obsolete("Use OpenApiJsonWriter(TextWriter textWriter, OpenApiJsonWriterSettings settings) " +
800+
"instead.")]
801+
public OpenApiJsonWriter(System.IO.TextWriter textWriter, Microsoft.OpenApi.OpenApiWriterSettings? settings, bool terseOutput) { }
799802
protected override int BaseIndentation { get; }
800803
public override void WriteEndArray() { }
801804
public override void WriteEndObject() { }

0 commit comments

Comments
 (0)