-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathOpenApiHeaderReferenceTests.cs
196 lines (176 loc) · 6.81 KB
/
OpenApiHeaderReferenceTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.References;
using Microsoft.OpenApi.Reader;
using Microsoft.OpenApi.YamlReader;
using Microsoft.OpenApi.Writers;
using VerifyXunit;
using Xunit;
namespace Microsoft.OpenApi.Tests.Models.References
{
[Collection("DefaultSettings")]
public class OpenApiHeaderReferenceTests
{
// OpenApi doc with external $ref
private const string OpenApi = @"
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
servers:
- url: https://myserver.com/v1.0
paths:
/users:
post:
summary: Create a post
responses:
'201':
description: Post created successfully
headers:
Location:
$ref: 'https://myserver.com/beta##/components/headers/LocationHeader'
components:
schemas:
User:
type: object
properties:
id:
type: integer
name:
type: string
";
// OpenApi doc with local $ref
private const string OpenApi_2 = @"
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
servers:
- url: https://myserver.com/beta
paths:
/users:
post:
summary: Create a post
responses:
'201':
description: Post created successfully
headers:
Location:
$ref: '#/components/headers/LocationHeader'
components:
headers:
LocationHeader:
description: The URL of the newly created post
schema:
type: string
";
private readonly OpenApiHeaderReference _localHeaderReference;
private readonly OpenApiHeaderReference _externalHeaderReference;
private readonly OpenApiDocument _openApiDoc;
private readonly OpenApiDocument _openApiDoc_2;
public OpenApiHeaderReferenceTests()
{
_openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml, SettingsFixture.ReaderSettings).Document;
_openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml, SettingsFixture.ReaderSettings).Document;
_openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri);
_openApiDoc.Workspace.RegisterComponents(_openApiDoc_2);
_localHeaderReference = new OpenApiHeaderReference("LocationHeader", _openApiDoc_2)
{
Description = "Location of the locally referenced post"
};
_externalHeaderReference = new OpenApiHeaderReference("LocationHeader", _openApiDoc, "https://myserver.com/beta")
{
Description = "Location of the externally referenced post"
};
}
[Fact]
public void HeaderReferenceResolutionWorks()
{
// Assert
Assert.Equal(JsonSchemaType.String, _externalHeaderReference.Schema.Type);
Assert.Equal("Location of the locally referenced post", _localHeaderReference.Description);
Assert.Equal("Location of the externally referenced post", _externalHeaderReference.Description);
Assert.Equal("The URL of the newly created post",
_openApiDoc_2.Components.Headers.First().Value.Description); // The main description value shouldn't change
}
[Theory]
[InlineData(true, true)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public async Task SerializeHeaderReferenceAsV3JsonWorks(bool produceTerseOutput, bool inlineLocalReferences)
{
// Arrange
var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
var writer = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = produceTerseOutput, InlineLocalReferences = inlineLocalReferences });
// Act
_localHeaderReference.SerializeAsV3(writer);
await writer.FlushAsync();
// Assert
await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput, inlineLocalReferences);
}
[Theory]
[InlineData(true, true)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public async Task SerializeHeaderReferenceAsV31JsonWorks(bool produceTerseOutput, bool inlineLocalReferences)
{
// Arrange
var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
var writer = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = produceTerseOutput, InlineLocalReferences = inlineLocalReferences });
// Act
_localHeaderReference.SerializeAsV31(writer);
await writer.FlushAsync();
// Assert
await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput, inlineLocalReferences);
}
[Theory]
[InlineData(true, true)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public async Task SerializeHeaderReferenceAsV2JsonWorksAsync(bool produceTerseOutput, bool inlineLocalReferences)
{
// Arrange
var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
var writer = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = produceTerseOutput, InlineLocalReferences = inlineLocalReferences });
// Act
_localHeaderReference.SerializeAsV2(writer);
await writer.FlushAsync();
// Assert
await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput, inlineLocalReferences);
}
[Fact]
public void OpenApiHeaderTargetShouldResolveReference()
{
var doc = new OpenApiDocument
{
Components = new OpenApiComponents
{
Headers =
{
{ "header1", new OpenApiHeader
{
Description = "test header",
Schema = new OpenApiSchema
{
Type = JsonSchemaType.String
}
}
}
}
}
};
doc.Workspace.RegisterComponents(doc);
var headerReference = new OpenApiHeaderReference("header1", doc);
Assert.Equal("test header", headerReference.Description);
Assert.Equal(JsonSchemaType.String, headerReference.Schema.Type);
}
}
}