Skip to content

Commit a08028f

Browse files
authored
Merge pull request #640 from nulltoken/ntk/terse
Teach OpenApiJsonWriter to produce a terse output
2 parents 89439ee + b800ae0 commit a08028f

File tree

127 files changed

+2767
-2554
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+2767
-2554
lines changed

src/Microsoft.OpenApi/Writers/OpenApiJsonWriter.cs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ public OpenApiJsonWriter(TextWriter textWriter) : base(textWriter, null)
1818
{
1919
}
2020

21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="OpenApiJsonWriter"/> class.
23+
/// </summary>
24+
/// <param name="textWriter">The text writer.</param>
25+
/// <param name="settings">Settings for controlling how the OpenAPI document will be written out.</param>
26+
public OpenApiJsonWriter(TextWriter textWriter, OpenApiJsonWriterSettings settings) : base(textWriter, settings)
27+
{
28+
_produceTerseOutput = settings.Terse;
29+
}
30+
2131
/// <summary>
2232
/// Initializes a new instance of the <see cref="OpenApiJsonWriter"/> class.
2333
/// </summary>
@@ -27,6 +37,11 @@ public OpenApiJsonWriter(TextWriter textWriter, OpenApiWriterSettings settings)
2737
{
2838
}
2939

40+
/// <summary>
41+
/// Indicates whether or not the produced document will be written in a compact or pretty fashion.
42+
/// </summary>
43+
private bool _produceTerseOutput = false;
44+
3045
/// <summary>
3146
/// Base Indentation Level.
3247
/// This denotes how many indentations are needed for the property in the base object.
@@ -51,7 +66,7 @@ public override void WriteStartObject()
5166
Writer.Write(WriterConstants.ArrayElementSeparator);
5267
}
5368

54-
Writer.WriteLine();
69+
WriteLine();
5570
WriteIndentation();
5671
}
5772

@@ -68,13 +83,16 @@ public override void WriteEndObject()
6883
var currentScope = EndScope(ScopeType.Object);
6984
if (currentScope.ObjectCount != 0)
7085
{
71-
Writer.WriteLine();
86+
WriteLine();
7287
DecreaseIndentation();
7388
WriteIndentation();
7489
}
7590
else
7691
{
77-
Writer.Write(WriterConstants.WhiteSpaceForEmptyObject);
92+
if (!_produceTerseOutput)
93+
{
94+
Writer.Write(WriterConstants.WhiteSpaceForEmptyObject);
95+
}
7896
DecreaseIndentation();
7997
}
8098

@@ -99,7 +117,7 @@ public override void WriteStartArray()
99117
Writer.Write(WriterConstants.ArrayElementSeparator);
100118
}
101119

102-
Writer.WriteLine();
120+
WriteLine();
103121
WriteIndentation();
104122
}
105123

@@ -115,7 +133,7 @@ public override void WriteEndArray()
115133
var current = EndScope(ScopeType.Array);
116134
if (current.ObjectCount != 0)
117135
{
118-
Writer.WriteLine();
136+
WriteLine();
119137
DecreaseIndentation();
120138
WriteIndentation();
121139
}
@@ -143,7 +161,7 @@ public override void WritePropertyName(string name)
143161
Writer.Write(WriterConstants.ObjectMemberSeparator);
144162
}
145163

146-
Writer.WriteLine();
164+
WriteLine();
147165

148166
currentScope.ObjectCount++;
149167

@@ -154,6 +172,11 @@ public override void WritePropertyName(string name)
154172
Writer.Write(name);
155173

156174
Writer.Write(WriterConstants.NameValueSeparator);
175+
176+
if (!_produceTerseOutput)
177+
{
178+
Writer.Write(WriterConstants.NameValueSeparatorWhiteSpaceSuffix);
179+
}
157180
}
158181

159182
/// <summary>
@@ -198,7 +221,7 @@ protected override void WriteValueSeparator()
198221
Writer.Write(WriterConstants.ArrayElementSeparator);
199222
}
200223

201-
Writer.WriteLine();
224+
WriteLine();
202225
WriteIndentation();
203226
currentScope.ObjectCount++;
204227
}
@@ -212,5 +235,31 @@ public override void WriteRaw(string value)
212235
WriteValueSeparator();
213236
Writer.Write(value);
214237
}
238+
239+
/// <summary>
240+
/// Write the indentation.
241+
/// </summary>
242+
public override void WriteIndentation()
243+
{
244+
if (_produceTerseOutput)
245+
{
246+
return;
247+
}
248+
249+
base.WriteIndentation();
250+
}
251+
252+
/// <summary>
253+
/// Writes a line terminator to the text string or stream.
254+
/// </summary>
255+
private void WriteLine()
256+
{
257+
if (_produceTerseOutput)
258+
{
259+
return;
260+
}
261+
262+
Writer.WriteLine();
263+
}
215264
}
216265
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Microsoft.OpenApi.Writers
2+
{
3+
/// <summary>
4+
/// Configuration settings to control how OpenAPI Json documents are written
5+
/// </summary>
6+
public class OpenApiJsonWriterSettings : OpenApiWriterSettings
7+
{
8+
/// <summary>
9+
/// Initializes a new instance of the <see cref="OpenApiJsonWriterSettings"/> class.
10+
/// </summary>
11+
public OpenApiJsonWriterSettings()
12+
{ }
13+
14+
/// <summary>
15+
/// Indicates whether or not the produced document will be written in a compact or pretty fashion.
16+
/// </summary>
17+
public bool Terse { get; set; } = false;
18+
}
19+
}

src/Microsoft.OpenApi/Writers/WriterConstants.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ internal static class WriterConstants
8181
/// <summary>
8282
/// The separator between the name and the value.
8383
/// </summary>
84-
internal const string NameValueSeparator = ": ";
84+
internal const string NameValueSeparator = ":";
85+
86+
/// <summary>
87+
/// The white space postfixing <see cref="NameValueSeparator"/>
88+
/// when producing pretty content.
89+
/// </summary>
90+
internal const string NameValueSeparatorWhiteSpaceSuffix = " ";
8591

8692
/// <summary>
8793
/// The white space for empty object

test/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.received.*

test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net462;net50</TargetFrameworks>
3+
<TargetFrameworks>net48;net50</TargetFrameworks>
44
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
55
<Authors></Authors>
66
<Company>Microsoft</Company>

test/Microsoft.OpenApi.SmokeTests/Microsoft.OpenApi.SmokeTests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net462</TargetFrameworks>
4+
<TargetFrameworks>net48</TargetFrameworks>
55
</PropertyGroup>
66

77
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
8-
<DefineConstants>TRACE;DEBUG;net462</DefineConstants>
8+
<DefineConstants>TRACE;DEBUG;NET48</DefineConstants>
99
</PropertyGroup>
1010

1111
<ItemGroup>

test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net462;net50</TargetFrameworks>
3+
<TargetFrameworks>net48;net50</TargetFrameworks>
44
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
55
<Authors></Authors>
66
<Company>Microsoft</Company>
@@ -19,6 +19,8 @@
1919
<PackageReference Include="FluentAssertions" Version="5.10.3" />
2020
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
2121
<PackageReference Include="SharpYaml" Version="1.8.0" />
22+
<PackageReference Include="Verify" Version="13.3.1" />
23+
<PackageReference Include="Verify.Xunit" Version="13.3.1" />
2224
<PackageReference Include="xunit" Version="2.4.1" />
2325
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
2426
<PrivateAssets>all</PrivateAssets>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"$request.body#/url": {
3+
"post": {
4+
"requestBody": {
5+
"content": {
6+
"application/json": {
7+
"schema": {
8+
"type": "object"
9+
}
10+
}
11+
}
12+
},
13+
"responses": {
14+
"200": {
15+
"description": "Success"
16+
}
17+
}
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"$request.body#/url":{"post":{"requestBody":{"content":{"application/json":{"schema":{"type":"object"}}}},"responses":{"200":{"description":"Success"}}}}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"$request.body#/url": {
3+
"post": {
4+
"requestBody": {
5+
"content": {
6+
"application/json": {
7+
"schema": {
8+
"type": "object"
9+
}
10+
}
11+
}
12+
},
13+
"responses": {
14+
"200": {
15+
"description": "Success"
16+
}
17+
}
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)