Skip to content

Commit 4eb9a13

Browse files
committed
Add test
1 parent cd392b7 commit 4eb9a13

File tree

3 files changed

+305
-3
lines changed

3 files changed

+305
-3
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@
125125
<EmbeddedResource Include="V3Tests\Samples\OpenApiCallback\basicCallback.yaml">
126126
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
127127
</EmbeddedResource>
128+
<EmbeddedResource Include="V3Tests\Samples\OpenApiDocument\documentWithReusablePaths.yaml">
129+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
130+
</EmbeddedResource>
128131
<EmbeddedResource Include="V3Tests\Samples\OpenApiDiscriminator\basicDiscriminator.yaml">
129132
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
130133
</EmbeddedResource>

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs

Lines changed: 218 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
using System.Threading;
1010
using FluentAssertions;
1111
using Microsoft.OpenApi.Any;
12-
using Microsoft.OpenApi.Extensions;
1312
using Microsoft.OpenApi.Interfaces;
1413
using Microsoft.OpenApi.Models;
1514
using Microsoft.OpenApi.Validations;
1615
using Microsoft.OpenApi.Validations.Rules;
1716
using Microsoft.OpenApi.Writers;
18-
using Newtonsoft.Json;
1917
using Xunit;
2018
using Xunit.Abstractions;
2119

@@ -1256,7 +1254,7 @@ public void GlobalSecurityRequirementShouldReferenceSecurityScheme()
12561254
Assert.Same(securityRequirement.Keys.First(), openApiDoc.Components.SecuritySchemes.First().Value);
12571255
}
12581256
}
1259-
1257+
12601258
[Fact]
12611259
public void HeaderParameterShouldAllowExample()
12621260
{
@@ -1535,5 +1533,222 @@ public void ParseDocumentWithWebhooksShouldSucceed()
15351533
diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
15361534
actual.Should().BeEquivalentTo(expected);
15371535
}
1536+
1537+
[Fact]
1538+
public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds()
1539+
{
1540+
// Arrange && Act
1541+
using var stream = Resources.GetStream("V3Tests/Samples/OpenApiDocument/documentWithReusablePaths.yaml");
1542+
var actual = new OpenApiStreamReader().Read(stream, out var context);
1543+
1544+
var components = new OpenApiComponents
1545+
{
1546+
Schemas = new Dictionary<string, OpenApiSchema>
1547+
{
1548+
["pet"] = new OpenApiSchema
1549+
{
1550+
Type = "object",
1551+
Required = new HashSet<string>
1552+
{
1553+
"id",
1554+
"name"
1555+
},
1556+
Properties = new Dictionary<string, OpenApiSchema>
1557+
{
1558+
["id"] = new OpenApiSchema
1559+
{
1560+
Type = "integer",
1561+
Format = "int64"
1562+
},
1563+
["name"] = new OpenApiSchema
1564+
{
1565+
Type = "string"
1566+
},
1567+
["tag"] = new OpenApiSchema
1568+
{
1569+
Type = "string"
1570+
},
1571+
},
1572+
Reference = new OpenApiReference
1573+
{
1574+
Type = ReferenceType.Schema,
1575+
Id = "pet",
1576+
HostDocument = actual
1577+
}
1578+
},
1579+
["newPet"] = new OpenApiSchema
1580+
{
1581+
Type = "object",
1582+
Required = new HashSet<string>
1583+
{
1584+
"name"
1585+
},
1586+
Properties = new Dictionary<string, OpenApiSchema>
1587+
{
1588+
["id"] = new OpenApiSchema
1589+
{
1590+
Type = "integer",
1591+
Format = "int64"
1592+
},
1593+
["name"] = new OpenApiSchema
1594+
{
1595+
Type = "string"
1596+
},
1597+
["tag"] = new OpenApiSchema
1598+
{
1599+
Type = "string"
1600+
},
1601+
},
1602+
Reference = new OpenApiReference
1603+
{
1604+
Type = ReferenceType.Schema,
1605+
Id = "newPet",
1606+
HostDocument = actual
1607+
}
1608+
}
1609+
}
1610+
};
1611+
1612+
// Create a clone of the schema to avoid modifying things in components.
1613+
var petSchema = Clone(components.Schemas["pet"]);
1614+
1615+
petSchema.Reference = new OpenApiReference
1616+
{
1617+
Id = "pet",
1618+
Type = ReferenceType.Schema,
1619+
HostDocument = actual
1620+
};
1621+
1622+
var newPetSchema = Clone(components.Schemas["newPet"]);
1623+
1624+
newPetSchema.Reference = new OpenApiReference
1625+
{
1626+
Id = "newPet",
1627+
Type = ReferenceType.Schema,
1628+
HostDocument = actual
1629+
};
1630+
components.PathItems = new Dictionary<string, OpenApiPathItem>
1631+
{
1632+
["/pets"] = new OpenApiPathItem
1633+
{
1634+
Operations = new Dictionary<OperationType, OpenApiOperation>
1635+
{
1636+
[OperationType.Get] = new OpenApiOperation
1637+
{
1638+
Description = "Returns all pets from the system that the user has access to",
1639+
OperationId = "findPets",
1640+
Parameters = new List<OpenApiParameter>
1641+
{
1642+
new OpenApiParameter
1643+
{
1644+
Name = "tags",
1645+
In = ParameterLocation.Query,
1646+
Description = "tags to filter by",
1647+
Required = false,
1648+
Schema = new OpenApiSchema
1649+
{
1650+
Type = "array",
1651+
Items = new OpenApiSchema
1652+
{
1653+
Type = "string"
1654+
}
1655+
}
1656+
},
1657+
new OpenApiParameter
1658+
{
1659+
Name = "limit",
1660+
In = ParameterLocation.Query,
1661+
Description = "maximum number of results to return",
1662+
Required = false,
1663+
Schema = new OpenApiSchema
1664+
{
1665+
Type = "integer",
1666+
Format = "int32"
1667+
}
1668+
}
1669+
},
1670+
Responses = new OpenApiResponses
1671+
{
1672+
["200"] = new OpenApiResponse
1673+
{
1674+
Description = "pet response",
1675+
Content = new Dictionary<string, OpenApiMediaType>
1676+
{
1677+
["application/json"] = new OpenApiMediaType
1678+
{
1679+
Schema = new OpenApiSchema
1680+
{
1681+
Type = "array",
1682+
Items = petSchema
1683+
}
1684+
},
1685+
["application/xml"] = new OpenApiMediaType
1686+
{
1687+
Schema = new OpenApiSchema
1688+
{
1689+
Type = "array",
1690+
Items = petSchema
1691+
}
1692+
}
1693+
}
1694+
}
1695+
}
1696+
},
1697+
[OperationType.Post] = new OpenApiOperation
1698+
{
1699+
RequestBody = new OpenApiRequestBody
1700+
{
1701+
Description = "Information about a new pet in the system",
1702+
Required = true,
1703+
Content = new Dictionary<string, OpenApiMediaType>
1704+
{
1705+
["application/json"] = new OpenApiMediaType
1706+
{
1707+
Schema = newPetSchema
1708+
}
1709+
}
1710+
},
1711+
Responses = new OpenApiResponses
1712+
{
1713+
["200"] = new OpenApiResponse
1714+
{
1715+
Description = "Return a 200 status to indicate that the data was received successfully",
1716+
Content = new Dictionary<string, OpenApiMediaType>
1717+
{
1718+
["application/json"] = new OpenApiMediaType
1719+
{
1720+
Schema = petSchema
1721+
},
1722+
}
1723+
}
1724+
}
1725+
}
1726+
},
1727+
Reference = new OpenApiReference
1728+
{
1729+
Type = ReferenceType.PathItem,
1730+
Id = "/pets",
1731+
HostDocument = actual
1732+
}
1733+
}
1734+
};
1735+
1736+
var expected = new OpenApiDocument
1737+
{
1738+
Info = new OpenApiInfo
1739+
{
1740+
Title = "Webhook Example",
1741+
Version = "1.0.0"
1742+
},
1743+
Webhooks = components.PathItems,
1744+
Components = components
1745+
};
1746+
1747+
// Assert
1748+
actual.Should().BeEquivalentTo(expected);
1749+
context.Should().BeEquivalentTo(
1750+
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_0 });
1751+
1752+
}
15381753
}
15391754
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
openapi : 3.1.0
2+
info:
3+
title: Webhook Example
4+
version: 1.0.0
5+
webhooks:
6+
/pets:
7+
"$ref": '#/components/pathItems/pets'
8+
components:
9+
schemas:
10+
pet:
11+
type: object
12+
required:
13+
- id
14+
- name
15+
properties:
16+
id:
17+
type: integer
18+
format: int64
19+
name:
20+
type: string
21+
tag:
22+
type: string
23+
newPet:
24+
type: object
25+
required:
26+
- name
27+
properties:
28+
id:
29+
type: integer
30+
format: int64
31+
name:
32+
type: string
33+
tag:
34+
type: string
35+
pathItems:
36+
/pets:
37+
get:
38+
description: Returns all pets from the system that the user has access to
39+
operationId: findPets
40+
parameters:
41+
- name: tags
42+
in: query
43+
description: tags to filter by
44+
required: false
45+
schema:
46+
type: array
47+
items:
48+
type: string
49+
- name: limit
50+
in: query
51+
description: maximum number of results to return
52+
required: false
53+
schema:
54+
type: integer
55+
format: int32
56+
responses:
57+
'200':
58+
description: pet response
59+
content:
60+
application/json:
61+
schema:
62+
type: array
63+
items:
64+
"$ref": '#/components/schemas/pet'
65+
application/xml:
66+
schema:
67+
type: array
68+
items:
69+
"$ref": '#/components/schemas/pet'
70+
post:
71+
requestBody:
72+
description: Information about a new pet in the system
73+
required: true
74+
content:
75+
'application/json':
76+
schema:
77+
"$ref": '#/components/schemas/newPet'
78+
responses:
79+
"200":
80+
description: Return a 200 status to indicate that the data was received successfully
81+
content:
82+
application/json:
83+
schema:
84+
$ref: '#/components/schemas/pet'

0 commit comments

Comments
 (0)