Skip to content

Commit 2d48b61

Browse files
committed
Adds a flag for identifying whether an external reference is a fragment
1 parent 7637861 commit 2d48b61

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/Microsoft.OpenApi.Readers/V3/OpenApiV3VersionService.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public OpenApiReference ConvertToOpenApiReference(
7171
string reference,
7272
ReferenceType? type)
7373
{
74+
var openApiReference = new OpenApiReference();
7475
if (!string.IsNullOrWhiteSpace(reference))
7576
{
7677
var segments = reference.Split('#');
@@ -127,13 +128,16 @@ public OpenApiReference ConvertToOpenApiReference(
127128
}
128129
id = localSegments[3];
129130
}
130-
131-
return new OpenApiReference
131+
else
132132
{
133-
ExternalResource = segments[0],
134-
Type = type,
135-
Id = id
136-
};
133+
openApiReference.IsFragrament = true;
134+
}
135+
136+
openApiReference.ExternalResource = segments[0];
137+
openApiReference.Type = type;
138+
openApiReference.Id = id;
139+
140+
return openApiReference;
137141
}
138142
}
139143

src/Microsoft.OpenApi/Models/OpenApiReference.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class OpenApiReference : IOpenApiSerializable
4545
/// </summary>
4646
public bool IsLocal => ExternalResource == null;
4747

48+
/// <summary>
49+
/// Gets a flag indicating whether a file is a valid OpenAPI document or a fragment
50+
/// </summary>
51+
public bool IsFragrament = false;
52+
4853
/// <summary>
4954
/// The OpenApiDocument that is hosting the OpenApiReference instance. This is used to enable dereferencing the reference.
5055
/// </summary>
@@ -196,7 +201,12 @@ private string GetExternalReferenceV3()
196201
{
197202
if (Id != null)
198203
{
199-
return ExternalResource + "#/components/" + Type.GetDisplayName() + "/"+ Id;
204+
if (IsFragrament)
205+
{
206+
return ExternalResource + "#" + Id;
207+
}
208+
209+
return ExternalResource + "#/components/" + Type.GetDisplayName() + "/"+ Id;
200210
}
201211

202212
return ExternalResource;

0 commit comments

Comments
 (0)