6
6
using System . Collections . Generic ;
7
7
using System . Globalization ;
8
8
using System . IO ;
9
+ using System . Linq ;
9
10
using FluentAssertions ;
10
11
using Microsoft . OpenApi . Writers ;
11
12
using Newtonsoft . Json ;
@@ -24,36 +25,36 @@ public OpenApiJsonWriterTests(ITestOutputHelper output)
24
25
_output = output ;
25
26
}
26
27
28
+ static bool [ ] shouldProduceTerseOutputValues = new [ ] { true , false } ;
29
+
27
30
public static IEnumerable < object [ ] > WriteStringListAsJsonShouldMatchExpectedTestCases ( )
28
31
{
29
- yield return new object [ ]
30
- {
31
- new [ ]
32
- {
33
- "string1" ,
34
- "string2" ,
35
- "string3" ,
36
- "string4" ,
37
- "string5" ,
38
- "string6" ,
39
- "string7" ,
40
- "string8"
32
+ return
33
+ from input in new string [ ] [ ] {
34
+ new [ ]
35
+ {
36
+ "string1" ,
37
+ "string2" ,
38
+ "string3" ,
39
+ "string4" ,
40
+ "string5" ,
41
+ "string6" ,
42
+ "string7" ,
43
+ "string8"
44
+ } ,
45
+ new [ ] { "string1" , "string1" , "string1" , "string1" }
41
46
}
42
- } ;
43
-
44
- yield return new object [ ]
45
- {
46
- new [ ] { "string1" , "string1" , "string1" , "string1" }
47
- } ;
47
+ from shouldBeTerse in shouldProduceTerseOutputValues
48
+ select new object [ ] { input , shouldBeTerse } ;
48
49
}
49
50
50
51
[ Theory ]
51
52
[ MemberData ( nameof ( WriteStringListAsJsonShouldMatchExpectedTestCases ) ) ]
52
- public void WriteStringListAsJsonShouldMatchExpected ( string [ ] stringValues )
53
+ public void WriteStringListAsJsonShouldMatchExpected ( string [ ] stringValues , bool produceTerseOutput )
53
54
{
54
55
// Arrange
55
56
var outputString = new StringWriter ( CultureInfo . InvariantCulture ) ;
56
- var writer = new OpenApiJsonWriter ( outputString ) ;
57
+ var writer = new OpenApiJsonWriter ( outputString , new OpenApiJsonWriterSettings { Terse = produceTerseOutput } ) ;
57
58
58
59
// Act
59
60
writer . WriteStartArray ( ) ;
@@ -75,123 +76,112 @@ public void WriteStringListAsJsonShouldMatchExpected(string[] stringValues)
75
76
76
77
public static IEnumerable < object [ ] > WriteMapAsJsonShouldMatchExpectedTestCasesSimple ( )
77
78
{
78
- // Simple map
79
- yield return new object [ ]
80
- {
81
- new Dictionary < string , object >
82
- {
83
- [ "property1" ] = "value1" ,
84
- [ "property2" ] = "value2" ,
85
- [ "property3" ] = "value3" ,
86
- [ "property4" ] = "value4"
87
- }
88
- } ;
79
+ return
80
+ from input in new IDictionary < string , object > [ ] {
81
+ // Simple map
82
+ new Dictionary < string , object >
83
+ {
84
+ [ "property1" ] = "value1" ,
85
+ [ "property2" ] = "value2" ,
86
+ [ "property3" ] = "value3" ,
87
+ [ "property4" ] = "value4"
88
+ } ,
89
89
90
- // Simple map with duplicate values
91
- yield return new object [ ]
92
- {
93
- new Dictionary < string , object >
94
- {
95
- [ "property1" ] = "value1" ,
96
- [ "property2" ] = "value1" ,
97
- [ "property3" ] = "value1" ,
98
- [ "property4" ] = "value1"
90
+ // Simple map with duplicate values
91
+ new Dictionary < string , object >
92
+ {
93
+ [ "property1" ] = "value1" ,
94
+ [ "property2" ] = "value1" ,
95
+ [ "property3" ] = "value1" ,
96
+ [ "property4" ] = "value1"
97
+ } ,
99
98
}
100
- } ;
99
+ from shouldBeTerse in shouldProduceTerseOutputValues
100
+ select new object [ ] { input , shouldBeTerse } ;
101
101
}
102
102
103
103
public static IEnumerable < object [ ] > WriteMapAsJsonShouldMatchExpectedTestCasesComplex ( )
104
104
{
105
- // Empty map and empty list
106
- yield return new object [ ]
107
- {
108
- new Dictionary < string , object >
109
- {
110
- [ "property1" ] = new Dictionary < string , object > ( ) ,
111
- [ "property2" ] = new List < string > ( ) ,
112
- [ "property3" ] = new List < object >
105
+ return
106
+ from input in new IDictionary < string , object > [ ] {
107
+ // Empty map and empty list
108
+ new Dictionary < string , object >
113
109
{
114
- new Dictionary < string , object > ( ) ,
110
+ [ "property1" ] = new Dictionary < string , object > ( ) ,
111
+ [ "property2" ] = new List < string > ( ) ,
112
+ [ "property3" ] = new List < object >
113
+ {
114
+ new Dictionary < string , object > ( ) ,
115
+ } ,
116
+ [ "property4" ] = "value4"
115
117
} ,
116
- [ "property4" ] = "value4"
117
- }
118
- } ;
119
118
120
- // Number, boolean, and null handling
121
- yield return new object [ ]
122
- {
123
- new Dictionary < string , object >
124
- {
125
- [ "property1" ] = "10.0" ,
126
- [ "property2" ] = "10" ,
127
- [ "property3" ] = "-5" ,
128
- [ "property4" ] = 10.0M ,
129
- [ "property5" ] = 10 ,
130
- [ "property6" ] = - 5 ,
131
- [ "property7" ] = true ,
132
- [ "property8" ] = "true" ,
133
- [ "property9" ] = null ,
134
- [ "property10" ] = "null" ,
135
- [ "property11" ] = "" ,
136
- }
137
- } ;
138
-
139
- // DateTime
140
- yield return new object [ ]
141
- {
142
- new Dictionary < string , object >
143
- {
144
- [ "property1" ] = new DateTime ( 1970 , 01 , 01 ) ,
145
- [ "property2" ] = new DateTimeOffset ( new DateTime ( 1970 , 01 , 01 ) ) ,
146
- [ "property3" ] = new DateTime ( 2018 , 04 , 03 ) ,
147
- }
148
- } ;
119
+ // Number, boolean, and null handling
120
+ new Dictionary < string , object >
121
+ {
122
+ [ "property1" ] = "10.0" ,
123
+ [ "property2" ] = "10" ,
124
+ [ "property3" ] = "-5" ,
125
+ [ "property4" ] = 10.0M ,
126
+ [ "property5" ] = 10 ,
127
+ [ "property6" ] = - 5 ,
128
+ [ "property7" ] = true ,
129
+ [ "property8" ] = "true" ,
130
+ [ "property9" ] = null ,
131
+ [ "property10" ] = "null" ,
132
+ [ "property11" ] = "" ,
133
+ } ,
149
134
150
- // Nested map
151
- yield return new object [ ]
152
- {
153
- new Dictionary < string , object >
154
- {
155
- [ "property1" ] = new Dictionary < string , object >
135
+ // DateTime
136
+ new Dictionary < string , object >
156
137
{
157
- [ "innerProperty1" ] = "innerValue1"
138
+ [ "property1" ] = new DateTime ( 1970 , 01 , 01 ) ,
139
+ [ "property2" ] = new DateTimeOffset ( new DateTime ( 1970 , 01 , 01 ) ) ,
140
+ [ "property3" ] = new DateTime ( 2018 , 04 , 03 ) ,
158
141
} ,
159
- [ "property2" ] = "value2" ,
160
- [ "property3" ] = new Dictionary < string , object >
142
+
143
+ // Nested map
144
+ new Dictionary < string , object >
161
145
{
162
- [ "innerProperty3" ] = "innerValue3"
146
+ [ "property1" ] = new Dictionary < string , object >
147
+ {
148
+ [ "innerProperty1" ] = "innerValue1"
149
+ } ,
150
+ [ "property2" ] = "value2" ,
151
+ [ "property3" ] = new Dictionary < string , object >
152
+ {
153
+ [ "innerProperty3" ] = "innerValue3"
154
+ } ,
155
+ [ "property4" ] = "value4"
163
156
} ,
164
- [ "property4" ] = "value4"
165
- }
166
- } ;
167
157
168
- // Nested map and list
169
- yield return new object [ ]
170
- {
171
- new Dictionary < string , object >
172
- {
173
- [ "property1" ] = new Dictionary < string , object > ( ) ,
174
- [ "property2" ] = new List < string > ( ) ,
175
- [ "property3" ] = new List < object >
158
+ // Nested map and list
159
+ new Dictionary < string , object >
176
160
{
177
- new Dictionary < string , object > ( ) ,
178
- "string1" ,
179
- new Dictionary < string , object >
161
+ [ "property1" ] = new Dictionary < string , object > ( ) ,
162
+ [ "property2" ] = new List < string > ( ) ,
163
+ [ "property3" ] = new List < object >
180
164
{
181
- [ "innerProperty1" ] = new List < object > ( ) ,
182
- [ "innerProperty2" ] = "string2 ",
183
- [ "innerProperty3" ] = new List < object >
165
+ new Dictionary < string , object > ( ) ,
166
+ "string1 ",
167
+ new Dictionary < string , object >
184
168
{
185
- new List < string >
169
+ [ "innerProperty1" ] = new List < object > ( ) ,
170
+ [ "innerProperty2" ] = "string2" ,
171
+ [ "innerProperty3" ] = new List < object >
186
172
{
187
- "string3"
173
+ new List < string >
174
+ {
175
+ "string3"
176
+ }
188
177
}
189
178
}
190
- }
179
+ } ,
180
+ [ "property4" ] = "value4"
191
181
} ,
192
- [ "property4" ] = "value4"
193
182
}
194
- } ;
183
+ from shouldBeTerse in shouldProduceTerseOutputValues
184
+ select new object [ ] { input , shouldBeTerse } ;
195
185
}
196
186
197
187
private void WriteValueRecursive ( OpenApiJsonWriter writer , object value )
@@ -233,11 +223,11 @@ private void WriteValueRecursive(OpenApiJsonWriter writer, object value)
233
223
[ Theory ]
234
224
[ MemberData ( nameof ( WriteMapAsJsonShouldMatchExpectedTestCasesSimple ) ) ]
235
225
[ MemberData ( nameof ( WriteMapAsJsonShouldMatchExpectedTestCasesComplex ) ) ]
236
- public void WriteMapAsJsonShouldMatchExpected ( IDictionary < string , object > inputMap )
226
+ public void WriteMapAsJsonShouldMatchExpected ( IDictionary < string , object > inputMap , bool produceTerseOutput )
237
227
{
238
228
// Arrange
239
229
var outputString = new StringWriter ( CultureInfo . InvariantCulture ) ;
240
- var writer = new OpenApiJsonWriter ( outputString ) ;
230
+ var writer = new OpenApiJsonWriter ( outputString , new OpenApiJsonWriterSettings { Terse = produceTerseOutput } ) ;
241
231
242
232
// Act
243
233
WriteValueRecursive ( writer , inputMap ) ;
@@ -251,34 +241,24 @@ public void WriteMapAsJsonShouldMatchExpected(IDictionary<string, object> inputM
251
241
252
242
public static IEnumerable < object [ ] > WriteDateTimeAsJsonTestCases ( )
253
243
{
254
- yield return new object [ ]
255
- {
256
- new DateTimeOffset ( 2018 , 1 , 1 , 10 , 20 , 30 , TimeSpan . Zero ) ,
257
- } ;
258
-
259
- yield return new object [ ]
260
- {
261
- new DateTimeOffset ( 2018 , 1 , 1 , 10 , 20 , 30 , 100 , TimeSpan . FromHours ( 14 ) ) ,
262
- } ;
263
-
264
- yield return new object [ ]
265
- {
266
- DateTimeOffset . UtcNow + TimeSpan . FromDays ( 4 )
267
- } ;
268
-
269
- yield return new object [ ]
270
- {
271
- DateTime . UtcNow + TimeSpan . FromDays ( 4 )
272
- } ;
244
+ return
245
+ from input in new DateTimeOffset [ ] {
246
+ new DateTimeOffset ( 2018 , 1 , 1 , 10 , 20 , 30 , TimeSpan . Zero ) ,
247
+ new DateTimeOffset ( 2018 , 1 , 1 , 10 , 20 , 30 , 100 , TimeSpan . FromHours ( 14 ) ) ,
248
+ DateTimeOffset . UtcNow + TimeSpan . FromDays ( 4 ) ,
249
+ DateTime . UtcNow + TimeSpan . FromDays ( 4 ) ,
250
+ }
251
+ from shouldBeTerse in shouldProduceTerseOutputValues
252
+ select new object [ ] { input , shouldBeTerse } ;
273
253
}
274
254
275
255
[ Theory ]
276
256
[ MemberData ( nameof ( WriteDateTimeAsJsonTestCases ) ) ]
277
- public void WriteDateTimeAsJsonShouldMatchExpected ( DateTimeOffset dateTimeOffset )
257
+ public void WriteDateTimeAsJsonShouldMatchExpected ( DateTimeOffset dateTimeOffset , bool produceTerseOutput )
278
258
{
279
259
// Arrange
280
260
var outputString = new StringWriter ( CultureInfo . InvariantCulture ) ;
281
- var writer = new OpenApiJsonWriter ( outputString ) ;
261
+ var writer = new OpenApiJsonWriter ( outputString , new OpenApiJsonWriterSettings { Terse = produceTerseOutput } ) ;
282
262
283
263
// Act
284
264
writer . WriteValue ( dateTimeOffset ) ;
0 commit comments