Skip to content

Commit

Permalink
Merge pull request #831 from microsoft/mk/add-terse-param
Browse files Browse the repository at this point in the history
Add --terse-output Commandline parameter for producing JSON in terse format
  • Loading branch information
darrelmiller authored Apr 19, 2022
2 parents ad5c872 + 13e8888 commit 84ed1bc
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static async Task<int> TransformOpenApiDocument(
bool cleanoutput,
string? version,
OpenApiFormat? format,
bool terseOutput,
LogLevel loglevel,
bool inlineLocal,
bool inlineExternal,
Expand Down Expand Up @@ -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"),
};
Expand Down
8 changes: 6 additions & 2 deletions src/Microsoft.OpenApi.Hidi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ static async Task Main(string[] args)
var formatOption = new Option<OpenApiFormat?>("--format", "File format");
formatOption.AddAlias("-f");

var terseOutputOption = new Option<bool>("--terse-output", "Produce terse json output");
terseOutputOption.AddAlias("-to");

var logLevelOption = new Option<LogLevel>("--loglevel", () => LogLevel.Information, "The log level to use when logging messages to the main output.");
logLevelOption.AddAlias("-ll");

Expand Down Expand Up @@ -74,6 +77,7 @@ static async Task Main(string[] args)
cleanOutputOption,
versionOption,
formatOption,
terseOutputOption,
logLevelOption,
filterByOperationIdsOption,
filterByTagsOption,
Expand All @@ -82,8 +86,8 @@ static async Task Main(string[] args)
inlineExternalOption
};

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

rootCommand.Add(transformCommand);
rootCommand.Add(validateCommand);
Expand Down
18 changes: 6 additions & 12 deletions src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,14 @@ public static void Serialize<T>(
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);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Microsoft.OpenApi/Writers/OpenApiJsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public OpenApiJsonWriter(TextWriter textWriter, OpenApiJsonWriterSettings settin
/// </summary>
/// <param name="textWriter">The text writer.</param>
/// <param name="settings">Settings for controlling how the OpenAPI document will be written out.</param>
public OpenApiJsonWriter(TextWriter textWriter, OpenApiWriterSettings settings) : base(textWriter, settings)
/// <param name="terseOutput"> Setting for allowing the JSON emitted to be in terse format.</param>
public OpenApiJsonWriter(TextWriter textWriter, OpenApiWriterSettings settings, bool terseOutput = false) : base(textWriter, settings)
{
_produceTerseOutput = terseOutput;
}

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.OpenApi/Writers/OpenApiWriterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public ReferenceInlineSetting ReferenceInline {
/// </summary>
public bool InlineExternalReferences { get; set; } = false;


internal bool ShouldInlineReference(OpenApiReference reference)
{
return (reference.IsLocal && InlineLocalReferences)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
Expand Down

0 comments on commit 84ed1bc

Please sign in to comment.