Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieKimani1 committed Aug 30, 2024
1 parent 8222496 commit 6db0216
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Microsoft.OpenApi/Services/CopyReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public override void Visit(IOpenApiReferenceable referenceable)
default:
break;
}

base.Visit(referenceable);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand Down Expand Up @@ -34,4 +34,10 @@
</Compile>
</ItemGroup>

<ItemGroup>
<None Update="UtilityFiles\docWithReusableHeadersAndExamples.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
using Microsoft.OpenApi.Services;
using Microsoft.OpenApi.Tests.UtilityFiles;
using Moq;
using SharpYaml.Tokens;
using Xunit;

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

[Fact]
public void CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly()
{
// Arrange
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UtilityFiles", "docWithReusableHeadersAndExamples.yaml");
var operationIds = "getItems";

// Act
using var stream = File.OpenRead(filePath);
var doc = new OpenApiStreamReader().Read(stream, out var diagnostic);

var predicate = OpenApiFilterService.CreatePredicate(operationIds: operationIds);
var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(doc, predicate);

var response = subsetOpenApiDocument.Paths["/items"].Operations[OperationType.Get].Responses["200"];
var responseHeader = response.Headers["x-custom-header"];
var mediaTypeExample = response.Content["application/json"].Examples.First().Value;
var targetHeaders = subsetOpenApiDocument.Components.Headers;
var targetExamples = subsetOpenApiDocument.Components.Examples;

// Assert
Assert.False(responseHeader.UnresolvedReference);
Assert.False(mediaTypeExample.UnresolvedReference);
Assert.Single(targetHeaders);
Assert.Single(targetExamples);
}

[Theory]
[InlineData("reports.getTeamsUserActivityUserDetail-a3f1", null)]
[InlineData(null, "reports.Functions")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
openapi: 3.0.1
info:
title: Example with Multiple Operations and Local $refs
version: 1.0.0
paths:
/items:
get:
operationId: getItems
summary: Get a list of items
responses:
'200':
description: A list of items
headers:
x-custom-header:
$ref: '#/components/headers/CustomHeader'
content:
application/json:
schema:
type: array
items:
type: string
examples:
ItemExample:
$ref: '#/components/examples/ItemExample'
post:
operationId: createItem
summary: Create a new item
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
name:
type: string
example:
$ref: '#/components/examples/ItemExample'
responses:
'201':
description: Item created
content:
application/json:
schema:
type: object
properties:
id:
type: string
name:
type: string
example:
$ref: '#/components/examples/ItemExample'
components:
schemas:
pet:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
headers:
CustomHeader:
description: Custom header for authentication
required: true
schema:
type: string
examples:
ItemExample:
summary: Example of a new item to be created
value:
name: "New Item"

0 comments on commit 6db0216

Please sign in to comment.