Skip to content

Commit 84ed1bc

Browse files
authored
Merge pull request #831 from microsoft/mk/add-terse-param
Add --terse-output Commandline parameter for producing JSON in terse format
2 parents ad5c872 + 13e8888 commit 84ed1bc

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static async Task<int> TransformOpenApiDocument(
4444
bool cleanoutput,
4545
string? version,
4646
OpenApiFormat? format,
47+
bool terseOutput,
4748
LogLevel loglevel,
4849
bool inlineLocal,
4950
bool inlineExternal,
@@ -196,7 +197,7 @@ CancellationToken cancellationToken
196197

197198
IOpenApiWriter writer = openApiFormat switch
198199
{
199-
OpenApiFormat.Json => new OpenApiJsonWriter(textWriter, settings),
200+
OpenApiFormat.Json => terseOutput ? new OpenApiJsonWriter(textWriter, settings, terseOutput) : new OpenApiJsonWriter(textWriter, settings, false),
200201
OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings),
201202
_ => throw new ArgumentException("Unknown format"),
202203
};

src/Microsoft.OpenApi.Hidi/Program.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ static async Task Main(string[] args)
3939
var formatOption = new Option<OpenApiFormat?>("--format", "File format");
4040
formatOption.AddAlias("-f");
4141

42+
var terseOutputOption = new Option<bool>("--terse-output", "Produce terse json output");
43+
terseOutputOption.AddAlias("-to");
44+
4245
var logLevelOption = new Option<LogLevel>("--loglevel", () => LogLevel.Information, "The log level to use when logging messages to the main output.");
4346
logLevelOption.AddAlias("-ll");
4447

@@ -74,6 +77,7 @@ static async Task Main(string[] args)
7477
cleanOutputOption,
7578
versionOption,
7679
formatOption,
80+
terseOutputOption,
7781
logLevelOption,
7882
filterByOperationIdsOption,
7983
filterByTagsOption,
@@ -82,8 +86,8 @@ static async Task Main(string[] args)
8286
inlineExternalOption
8387
};
8488

85-
transformCommand.SetHandler<string, string, string, FileInfo, bool, string?, OpenApiFormat?, LogLevel, bool, bool, string, string, string, CancellationToken> (
86-
OpenApiService.TransformOpenApiDocument, descriptionOption, csdlOption, csdlFilterOption, outputOption, cleanOutputOption, versionOption, formatOption, logLevelOption, inlineLocalOption, inlineExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption);
89+
transformCommand.SetHandler<string, string, string, FileInfo, bool, string?, OpenApiFormat?, bool, LogLevel, bool, bool, string, string, string, CancellationToken> (
90+
OpenApiService.TransformOpenApiDocument, descriptionOption, csdlOption, csdlFilterOption, outputOption, cleanOutputOption, versionOption, formatOption, terseOutputOption, logLevelOption, inlineLocalOption, inlineExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption);
8791

8892
rootCommand.Add(transformCommand);
8993
rootCommand.Add(validateCommand);

src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs

+6-12
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,14 @@ public static void Serialize<T>(
8383
throw Error.ArgumentNull(nameof(stream));
8484
}
8585

86-
IOpenApiWriter writer;
8786
var streamWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture);
88-
switch (format)
89-
{
90-
case OpenApiFormat.Json:
91-
writer = new OpenApiJsonWriter(streamWriter,settings);
92-
break;
93-
case OpenApiFormat.Yaml:
94-
writer = new OpenApiYamlWriter(streamWriter, settings);
95-
break;
96-
default:
97-
throw new OpenApiException(string.Format(SRResource.OpenApiFormatNotSupported, format));
98-
}
9987

88+
IOpenApiWriter writer = format switch
89+
{
90+
OpenApiFormat.Json => new OpenApiJsonWriter(streamWriter, settings, false),
91+
OpenApiFormat.Yaml => new OpenApiYamlWriter(streamWriter, settings),
92+
_ => throw new OpenApiException(string.Format(SRResource.OpenApiFormatNotSupported, format)),
93+
};
10094
element.Serialize(writer, specVersion);
10195
}
10296

src/Microsoft.OpenApi/Writers/OpenApiJsonWriter.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ public OpenApiJsonWriter(TextWriter textWriter, OpenApiJsonWriterSettings settin
3333
/// </summary>
3434
/// <param name="textWriter">The text writer.</param>
3535
/// <param name="settings">Settings for controlling how the OpenAPI document will be written out.</param>
36-
public OpenApiJsonWriter(TextWriter textWriter, OpenApiWriterSettings settings) : base(textWriter, settings)
36+
/// <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)
3738
{
39+
_produceTerseOutput = terseOutput;
3840
}
3941

4042
/// <summary>

src/Microsoft.OpenApi/Writers/OpenApiWriterSettings.cs

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public ReferenceInlineSetting ReferenceInline {
6969
/// </summary>
7070
public bool InlineExternalReferences { get; set; } = false;
7171

72-
7372
internal bool ShouldInlineReference(OpenApiReference reference)
7473
{
7574
return (reference.IsLocal && InlineLocalReferences)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ namespace Microsoft.OpenApi.Writers
12811281
{
12821282
public OpenApiJsonWriter(System.IO.TextWriter textWriter) { }
12831283
public OpenApiJsonWriter(System.IO.TextWriter textWriter, Microsoft.OpenApi.Writers.OpenApiJsonWriterSettings settings) { }
1284-
public OpenApiJsonWriter(System.IO.TextWriter textWriter, Microsoft.OpenApi.Writers.OpenApiWriterSettings settings) { }
1284+
public OpenApiJsonWriter(System.IO.TextWriter textWriter, Microsoft.OpenApi.Writers.OpenApiWriterSettings settings, bool terseOutput = false) { }
12851285
protected override int BaseIndentation { get; }
12861286
public override void WriteEndArray() { }
12871287
public override void WriteEndObject() { }

0 commit comments

Comments
 (0)