Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Properties/SRResource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Properties/SRResource.resx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<value>Invalid Reference identifier '{0}'.</value>
</data>
<data name="InvalidReferenceType" xml:space="preserve">
<value>Invalid Reference Type.</value>
<value>Invalid Reference Type '{0}'.</value>
</data>
<data name="LocalReferenceRequiresType" xml:space="preserve">
<value>Local reference must have type specified.</value>
Expand Down
7 changes: 7 additions & 0 deletions src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ private void ResolveTags(IList<OpenApiTag> tags)
{
try
{
var referencedObject = typeof(T).Name;
var referenceType = reference?.Type.ToString();
if (referenceType is not null && !referencedObject.Contains(referenceType))
{
throw new OpenApiException(string.Format(Properties.SRResource.InvalidReferenceType, referenceType));
}

return _currentDocument.ResolveReference(reference, false) as T;
}
catch (OpenApiException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
using System.Threading;
using FluentAssertions;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.Interface;
using Microsoft.OpenApi.Validations;
using Microsoft.OpenApi.Validations.Rules;
using Microsoft.OpenApi.Writers;
Expand Down Expand Up @@ -1355,5 +1357,15 @@ public void ValidateExampleShouldNotHaveDataTypeMismatch()
var warnings = diagnostic.Warnings;
Assert.False(warnings.Any());
}

[Fact]
public void ParseDocumetWithWrongReferenceTypeShouldReturnADiagnosticError()
{
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "docWithWrongRef.json"));
_ = new OpenApiStreamReader().Read(stream, out var diagnostic);

diagnostic.Errors.Should().BeEquivalentTo(new List<OpenApiError> {
new( new OpenApiException("Invalid Reference Type 'Schema'.")) });
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"openapi":"3.0.0",
"info":{
"title":"some api",
"description":"some description",
"version": "1"
},
"servers":[{"url":"https://localhost"}],
"paths":{
"/count":{
"get":{
"responses":{
"200":{
"$ref":"#/components/schemas/count"
},
},
}
}
},
"components":{
"schemas":{
"count":{
"type": "number"
}
}
}
}