diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index feb62042b..584087ea7 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -44,6 +44,7 @@ public static async Task TransformOpenApiDocument( bool cleanoutput, string? version, OpenApiFormat? format, + bool terseOutput, LogLevel loglevel, bool inlineLocal, bool inlineExternal, @@ -196,7 +197,7 @@ CancellationToken cancellationToken IOpenApiWriter writer = openApiFormat switch { - OpenApiFormat.Json => new OpenApiJsonWriter(textWriter, settings), + OpenApiFormat.Json => terseOutput ? new OpenApiJsonWriter(textWriter, settings, terseOutput) : new OpenApiJsonWriter(textWriter, settings, false), OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings), _ => throw new ArgumentException("Unknown format"), }; diff --git a/src/Microsoft.OpenApi.Hidi/Program.cs b/src/Microsoft.OpenApi.Hidi/Program.cs index 8b466913c..80a4c2e14 100644 --- a/src/Microsoft.OpenApi.Hidi/Program.cs +++ b/src/Microsoft.OpenApi.Hidi/Program.cs @@ -39,6 +39,9 @@ static async Task Main(string[] args) var formatOption = new Option("--format", "File format"); formatOption.AddAlias("-f"); + var terseOutputOption = new Option("--terse-output", "Produce terse json output"); + terseOutputOption.AddAlias("-to"); + var logLevelOption = new Option("--loglevel", () => LogLevel.Information, "The log level to use when logging messages to the main output."); logLevelOption.AddAlias("-ll"); @@ -74,6 +77,7 @@ static async Task Main(string[] args) cleanOutputOption, versionOption, formatOption, + terseOutputOption, logLevelOption, filterByOperationIdsOption, filterByTagsOption, @@ -82,8 +86,8 @@ static async Task Main(string[] args) inlineExternalOption }; - transformCommand.SetHandler ( - OpenApiService.TransformOpenApiDocument, descriptionOption, csdlOption, csdlFilterOption, outputOption, cleanOutputOption, versionOption, formatOption, logLevelOption, inlineLocalOption, inlineExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption); + transformCommand.SetHandler ( + OpenApiService.TransformOpenApiDocument, descriptionOption, csdlOption, csdlFilterOption, outputOption, cleanOutputOption, versionOption, formatOption, terseOutputOption, logLevelOption, inlineLocalOption, inlineExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption); rootCommand.Add(transformCommand); rootCommand.Add(validateCommand); diff --git a/src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs b/src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs index 4694692ad..f60c5483b 100755 --- a/src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs +++ b/src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs @@ -83,20 +83,14 @@ public static void Serialize( throw Error.ArgumentNull(nameof(stream)); } - IOpenApiWriter writer; var streamWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture); - switch (format) - { - case OpenApiFormat.Json: - writer = new OpenApiJsonWriter(streamWriter,settings); - break; - case OpenApiFormat.Yaml: - writer = new OpenApiYamlWriter(streamWriter, settings); - break; - default: - throw new OpenApiException(string.Format(SRResource.OpenApiFormatNotSupported, format)); - } + IOpenApiWriter writer = format switch + { + OpenApiFormat.Json => new OpenApiJsonWriter(streamWriter, settings, false), + OpenApiFormat.Yaml => new OpenApiYamlWriter(streamWriter, settings), + _ => throw new OpenApiException(string.Format(SRResource.OpenApiFormatNotSupported, format)), + }; element.Serialize(writer, specVersion); } diff --git a/src/Microsoft.OpenApi/Writers/OpenApiJsonWriter.cs b/src/Microsoft.OpenApi/Writers/OpenApiJsonWriter.cs index 5454e8da8..10049974b 100644 --- a/src/Microsoft.OpenApi/Writers/OpenApiJsonWriter.cs +++ b/src/Microsoft.OpenApi/Writers/OpenApiJsonWriter.cs @@ -33,8 +33,10 @@ public OpenApiJsonWriter(TextWriter textWriter, OpenApiJsonWriterSettings settin /// /// The text writer. /// Settings for controlling how the OpenAPI document will be written out. - public OpenApiJsonWriter(TextWriter textWriter, OpenApiWriterSettings settings) : base(textWriter, settings) + /// Setting for allowing the JSON emitted to be in terse format. + public OpenApiJsonWriter(TextWriter textWriter, OpenApiWriterSettings settings, bool terseOutput = false) : base(textWriter, settings) { + _produceTerseOutput = terseOutput; } /// diff --git a/src/Microsoft.OpenApi/Writers/OpenApiWriterSettings.cs b/src/Microsoft.OpenApi/Writers/OpenApiWriterSettings.cs index 458d8f4a3..cf00c1339 100644 --- a/src/Microsoft.OpenApi/Writers/OpenApiWriterSettings.cs +++ b/src/Microsoft.OpenApi/Writers/OpenApiWriterSettings.cs @@ -69,7 +69,6 @@ public ReferenceInlineSetting ReferenceInline { /// public bool InlineExternalReferences { get; set; } = false; - internal bool ShouldInlineReference(OpenApiReference reference) { return (reference.IsLocal && InlineLocalReferences) diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index f589fa032..02400ddd7 100755 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -1281,7 +1281,7 @@ namespace Microsoft.OpenApi.Writers { public OpenApiJsonWriter(System.IO.TextWriter textWriter) { } public OpenApiJsonWriter(System.IO.TextWriter textWriter, Microsoft.OpenApi.Writers.OpenApiJsonWriterSettings settings) { } - public OpenApiJsonWriter(System.IO.TextWriter textWriter, Microsoft.OpenApi.Writers.OpenApiWriterSettings settings) { } + public OpenApiJsonWriter(System.IO.TextWriter textWriter, Microsoft.OpenApi.Writers.OpenApiWriterSettings settings, bool terseOutput = false) { } protected override int BaseIndentation { get; } public override void WriteEndArray() { } public override void WriteEndObject() { }