Skip to content

Commit b68a0a1

Browse files
committed
Ensure OpenApiSchemaTests cover terse output
1 parent 193098e commit b68a0a1

7 files changed

+81
-76
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"$ref": "#/components/schemas/schemaObject1"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"$ref":"#/components/schemas/schemaObject1"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"title": "title1",
3+
"multipleOf": 3,
4+
"maximum": 42,
5+
"minimum": 10,
6+
"exclusiveMinimum": true,
7+
"type": "integer",
8+
"default": 15,
9+
"nullable": true,
10+
"externalDocs": {
11+
"url": "http://example.com/externalDocs"
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"title":"title1","multipleOf":3,"maximum":42,"minimum":10,"exclusiveMinimum":true,"type":"integer","default":15,"nullable":true,"externalDocs":{"url":"http://example.com/externalDocs"}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"title": "title1",
3+
"required": [
4+
"property1"
5+
],
6+
"properties": {
7+
"property1": {
8+
"required": [
9+
"property3"
10+
],
11+
"properties": {
12+
"property2": {
13+
"type": "integer"
14+
},
15+
"property3": {
16+
"maxLength": 15,
17+
"type": "string"
18+
}
19+
}
20+
},
21+
"property4": {
22+
"properties": {
23+
"property5": {
24+
"properties": {
25+
"property6": {
26+
"type": "boolean"
27+
}
28+
}
29+
},
30+
"property7": {
31+
"minLength": 2,
32+
"type": "string"
33+
}
34+
},
35+
"readOnly": true
36+
}
37+
},
38+
"externalDocs": {
39+
"url": "http://example.com/externalDocs"
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"title":"title1","required":["property1"],"properties":{"property1":{"required":["property3"],"properties":{"property2":{"type":"integer"},"property3":{"maxLength":15,"type":"string"}}},"property4":{"properties":{"property5":{"properties":{"property6":{"type":"boolean"}}},"property7":{"minLength":2,"type":"string"}},"readOnly":true}},"externalDocs":{"url":"http://example.com/externalDocs"}}

test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs

Lines changed: 21 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
using System.Collections.Generic;
66
using System.Globalization;
77
using System.IO;
8+
using System.Threading.Tasks;
89
using FluentAssertions;
910
using Microsoft.OpenApi.Any;
1011
using Microsoft.OpenApi.Extensions;
1112
using Microsoft.OpenApi.Models;
1213
using Microsoft.OpenApi.Writers;
14+
using VerifyXunit;
1315
using Xunit;
1416
using Xunit.Abstractions;
1517

1618
namespace Microsoft.OpenApi.Tests.Models
1719
{
1820
[Collection("DefaultSettings")]
21+
[UsesVerify]
1922
public class OpenApiSchemaTests
2023
{
2124
public static OpenApiSchema BasicSchema = new OpenApiSchema();
@@ -365,117 +368,59 @@ public void SerializeAdvancedSchemaWithAllOfAsV3JsonWorks()
365368
actual.Should().Be(expected);
366369
}
367370

368-
[Fact]
369-
public void SerializeReferencedSchemaAsV3WithoutReferenceJsonWorks()
371+
[Theory]
372+
[InlineData(true)]
373+
[InlineData(false)]
374+
public async Task SerializeReferencedSchemaAsV3WithoutReferenceJsonWorksAsync(bool produceTerseOutput)
370375
{
371376
// Arrange
372377
var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
373-
var writer = new OpenApiJsonWriter(outputStringWriter);
378+
var writer = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = produceTerseOutput });
374379

375-
var expected = @"{
376-
""title"": ""title1"",
377-
""multipleOf"": 3,
378-
""maximum"": 42,
379-
""minimum"": 10,
380-
""exclusiveMinimum"": true,
381-
""type"": ""integer"",
382-
""default"": 15,
383-
""nullable"": true,
384-
""externalDocs"": {
385-
""url"": ""http://example.com/externalDocs""
386-
}
387-
}";
388380

389381
// Act
390382
ReferencedSchema.SerializeAsV3WithoutReference(writer);
391383
writer.Flush();
392384
var actual = outputStringWriter.GetStringBuilder().ToString();
393385

394386
// Assert
395-
actual = actual.MakeLineBreaksEnvironmentNeutral();
396-
expected = expected.MakeLineBreaksEnvironmentNeutral();
397-
actual.Should().Be(expected);
387+
await Verifier.Verify(actual).UseParameters(produceTerseOutput);
398388
}
399389

400-
[Fact]
401-
public void SerializeReferencedSchemaAsV3JsonWorks()
390+
[Theory]
391+
[InlineData(true)]
392+
[InlineData(false)]
393+
public async Task SerializeReferencedSchemaAsV3JsonWorksAsync(bool produceTerseOutput)
402394
{
403395
// Arrange
404396
var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
405-
var writer = new OpenApiJsonWriter(outputStringWriter);
406-
407-
var expected = @"{
408-
""$ref"": ""#/components/schemas/schemaObject1""
409-
}";
397+
var writer = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = produceTerseOutput });
410398

411399
// Act
412400
ReferencedSchema.SerializeAsV3(writer);
413401
writer.Flush();
414402
var actual = outputStringWriter.GetStringBuilder().ToString();
415403

416404
// Assert
417-
actual = actual.MakeLineBreaksEnvironmentNeutral();
418-
expected = expected.MakeLineBreaksEnvironmentNeutral();
419-
actual.Should().Be(expected);
405+
await Verifier.Verify(actual).UseParameters(produceTerseOutput);
420406
}
421407

422-
[Fact]
423-
public void SerializeSchemaWRequiredPropertiesAsV2JsonWorks()
408+
[Theory]
409+
[InlineData(true)]
410+
[InlineData(false)]
411+
public async Task SerializeSchemaWRequiredPropertiesAsV2JsonWorksAsync(bool produceTerseOutput)
424412
{
425413
// Arrange
426414
var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
427-
var writer = new OpenApiJsonWriter(outputStringWriter);
428-
var expected = @"{
429-
""title"": ""title1"",
430-
""required"": [
431-
""property1""
432-
],
433-
""properties"": {
434-
""property1"": {
435-
""required"": [
436-
""property3""
437-
],
438-
""properties"": {
439-
""property2"": {
440-
""type"": ""integer""
441-
},
442-
""property3"": {
443-
""maxLength"": 15,
444-
""type"": ""string""
445-
}
446-
}
447-
},
448-
""property4"": {
449-
""properties"": {
450-
""property5"": {
451-
""properties"": {
452-
""property6"": {
453-
""type"": ""boolean""
454-
}
455-
}
456-
},
457-
""property7"": {
458-
""minLength"": 2,
459-
""type"": ""string""
460-
}
461-
},
462-
""readOnly"": true
463-
}
464-
},
465-
""externalDocs"": {
466-
""url"": ""http://example.com/externalDocs""
467-
}
468-
}";
415+
var writer = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = produceTerseOutput });
469416

470417
// Act
471418
AdvancedSchemaWithRequiredPropertiesObject.SerializeAsV2(writer);
472419
writer.Flush();
473420
var actual = outputStringWriter.GetStringBuilder().ToString();
474421

475422
// Assert
476-
actual = actual.MakeLineBreaksEnvironmentNeutral();
477-
expected = expected.MakeLineBreaksEnvironmentNeutral();
478-
actual.Should().Be(expected);
423+
await Verifier.Verify(actual).UseParameters(produceTerseOutput);
479424
}
480425
}
481426
}

0 commit comments

Comments
 (0)