Skip to content

Commit f3f286c

Browse files
committed
Code clean up
1 parent 991b309 commit f3f286c

File tree

3 files changed

+90
-128
lines changed

3 files changed

+90
-128
lines changed

src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System;
54
using System.IO;
65
using System.Text.Json.Nodes;
76
using System.Text.Json;
@@ -12,7 +11,6 @@
1211
using Microsoft.OpenApi.Extensions;
1312
using Microsoft.OpenApi.Validations;
1413
using System.Linq;
15-
using System.Collections.Generic;
1614
using Microsoft.OpenApi.Services;
1715
using Microsoft.OpenApi.Interfaces;
1816
using Microsoft.OpenApi.Reader.Services;
@@ -95,7 +93,7 @@ public async Task<ReadResult> ReadAsync(JsonNode jsonNode,
9593
}
9694
}
9795

98-
ResolveReferences(diagnostic, document, settings);
96+
SetHostDocument(document);
9997
}
10098
catch (OpenApiException ex)
10199
{
@@ -189,28 +187,6 @@ private JsonNode LoadJsonNodes(TextReader input)
189187
return nodes;
190188
}
191189

192-
private void ResolveReferences(OpenApiDiagnostic diagnostic, OpenApiDocument document, OpenApiReaderSettings settings)
193-
{
194-
List<OpenApiError> errors = new();
195-
196-
// Resolve References if requested
197-
switch (settings.ReferenceResolution)
198-
{
199-
case ReferenceResolutionSetting.ResolveAllReferences:
200-
throw new ArgumentException("Resolving external references is not supported");
201-
case ReferenceResolutionSetting.ResolveLocalReferences:
202-
errors.AddRange(document.ResolveReferences());
203-
break;
204-
case ReferenceResolutionSetting.DoNotResolveReferences:
205-
break;
206-
}
207-
208-
foreach (var item in errors)
209-
{
210-
diagnostic.Errors.Add(item);
211-
}
212-
}
213-
214190
private async Task<OpenApiDiagnostic> LoadExternalRefs(OpenApiDocument document, CancellationToken cancellationToken, OpenApiReaderSettings settings, string format = null)
215191
{
216192
// Create workspace for all documents to live in.
@@ -221,5 +197,10 @@ private async Task<OpenApiDiagnostic> LoadExternalRefs(OpenApiDocument document,
221197
var workspaceLoader = new OpenApiWorkspaceLoader(openApiWorkSpace, settings.CustomExternalLoader ?? streamLoader, settings);
222198
return await workspaceLoader.LoadAsync(new OpenApiReference() { ExternalResource = "/" }, document, format ?? OpenApiConstants.Json, null, cancellationToken);
223199
}
200+
201+
private void SetHostDocument(OpenApiDocument document)
202+
{
203+
document.SetHostDocument();
204+
}
224205
}
225206
}

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

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Globalization;
33
using System.IO;
44
using FluentAssertions;
@@ -17,6 +17,11 @@ public class OpenApiDocumentTests
1717
{
1818
private const string SampleFolderPath = "V31Tests/Samples/OpenApiDocument/";
1919

20+
public OpenApiDocumentTests()
21+
{
22+
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Yaml, new OpenApiYamlReader());
23+
}
24+
2025
public static T Clone<T>(T element) where T : IOpenApiSerializable
2126
{
2227
using var stream = new MemoryStream();
@@ -177,7 +182,7 @@ public void ParseDocumentWithWebhooksShouldSucceed()
177182
};
178183

179184
// Assert
180-
var schema = actual.OpenApiDocument.Webhooks["/pets"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema;
185+
var schema = actual.OpenApiDocument.Webhooks["pets"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema;
181186
actual.OpenApiDiagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1 });
182187
actual.OpenApiDocument.Should().BeEquivalentTo(expected);
183188
}
@@ -301,7 +306,7 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds()
301306
Reference = new OpenApiReference
302307
{
303308
Type = ReferenceType.PathItem,
304-
Id = "/pets",
309+
Id = "pets",
305310
HostDocument = actual.OpenApiDocument
306311
}
307312
}
@@ -323,24 +328,11 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds()
323328
};
324329

325330
// Assert
326-
actual.OpenApiDocument.Should().BeEquivalentTo(expected);
331+
actual.OpenApiDocument.Should().BeEquivalentTo(expected, options => options.Excluding(x => x.Components.PathItems["pets"].Reference.HostDocument));
327332
actual.OpenApiDiagnostic.Should().BeEquivalentTo(
328333
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1 });
329334
}
330335

331-
[Fact]
332-
public void ParseDocumentWithDescriptionInDollarRefsShouldSucceed()
333-
{
334-
// Arrange
335-
var actual = OpenApiDocument.Load(Path.Combine(SampleFolderPath, "documentWithSummaryAndDescriptionInReference.yaml"));
336-
337-
// Act
338-
var header = actual.OpenApiDocument.Components.Responses["Test"].Headers["X-Test"];
339-
340-
// Assert
341-
Assert.True(header.Description == "A referenced X-Test header"); /*response header #ref's description overrides the header's description*/
342-
}
343-
344336
[Fact]
345337
public void ParseDocumentWithExampleInSchemaShouldSucceed()
346338
{

0 commit comments

Comments
 (0)