Skip to content

Commit 6db0216

Browse files
committed
Add test
1 parent 8222496 commit 6db0216

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

src/Microsoft.OpenApi/Services/CopyReferences.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public override void Visit(IOpenApiReferenceable referenceable)
110110
default:
111111
break;
112112
}
113+
113114
base.Visit(referenceable);
114115
}
115116

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

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

33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
@@ -34,4 +34,10 @@
3434
</Compile>
3535
</ItemGroup>
3636

37+
<ItemGroup>
38+
<None Update="UtilityFiles\docWithReusableHeadersAndExamples.yaml">
39+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
40+
</None>
41+
</ItemGroup>
42+
3743
</Project>

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
using Microsoft.Extensions.Logging;
55
using Microsoft.OpenApi.Models;
6+
using Microsoft.OpenApi.Readers;
67
using Microsoft.OpenApi.Services;
78
using Microsoft.OpenApi.Tests.UtilityFiles;
89
using Moq;
10+
using SharpYaml.Tokens;
911
using Xunit;
1012

1113
namespace Microsoft.OpenApi.Hidi.Tests
@@ -170,6 +172,33 @@ public void ThrowsInvalidOperationExceptionInCreatePredicateWhenInvalidArguments
170172
Assert.Equal("Cannot specify both operationIds and tags at the same time.", message2);
171173
}
172174

175+
[Fact]
176+
public void CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly()
177+
{
178+
// Arrange
179+
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UtilityFiles", "docWithReusableHeadersAndExamples.yaml");
180+
var operationIds = "getItems";
181+
182+
// Act
183+
using var stream = File.OpenRead(filePath);
184+
var doc = new OpenApiStreamReader().Read(stream, out var diagnostic);
185+
186+
var predicate = OpenApiFilterService.CreatePredicate(operationIds: operationIds);
187+
var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(doc, predicate);
188+
189+
var response = subsetOpenApiDocument.Paths["/items"].Operations[OperationType.Get].Responses["200"];
190+
var responseHeader = response.Headers["x-custom-header"];
191+
var mediaTypeExample = response.Content["application/json"].Examples.First().Value;
192+
var targetHeaders = subsetOpenApiDocument.Components.Headers;
193+
var targetExamples = subsetOpenApiDocument.Components.Examples;
194+
195+
// Assert
196+
Assert.False(responseHeader.UnresolvedReference);
197+
Assert.False(mediaTypeExample.UnresolvedReference);
198+
Assert.Single(targetHeaders);
199+
Assert.Single(targetExamples);
200+
}
201+
173202
[Theory]
174203
[InlineData("reports.getTeamsUserActivityUserDetail-a3f1", null)]
175204
[InlineData(null, "reports.Functions")]
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Example with Multiple Operations and Local $refs
4+
version: 1.0.0
5+
paths:
6+
/items:
7+
get:
8+
operationId: getItems
9+
summary: Get a list of items
10+
responses:
11+
'200':
12+
description: A list of items
13+
headers:
14+
x-custom-header:
15+
$ref: '#/components/headers/CustomHeader'
16+
content:
17+
application/json:
18+
schema:
19+
type: array
20+
items:
21+
type: string
22+
examples:
23+
ItemExample:
24+
$ref: '#/components/examples/ItemExample'
25+
post:
26+
operationId: createItem
27+
summary: Create a new item
28+
requestBody:
29+
required: true
30+
content:
31+
application/json:
32+
schema:
33+
type: object
34+
properties:
35+
name:
36+
type: string
37+
example:
38+
$ref: '#/components/examples/ItemExample'
39+
responses:
40+
'201':
41+
description: Item created
42+
content:
43+
application/json:
44+
schema:
45+
type: object
46+
properties:
47+
id:
48+
type: string
49+
name:
50+
type: string
51+
example:
52+
$ref: '#/components/examples/ItemExample'
53+
components:
54+
schemas:
55+
pet:
56+
type: object
57+
required:
58+
- id
59+
- name
60+
properties:
61+
id:
62+
type: integer
63+
format: int64
64+
name:
65+
type: string
66+
tag:
67+
type: string
68+
headers:
69+
CustomHeader:
70+
description: Custom header for authentication
71+
required: true
72+
schema:
73+
type: string
74+
examples:
75+
ItemExample:
76+
summary: Example of a new item to be created
77+
value:
78+
name: "New Item"
79+

0 commit comments

Comments
 (0)