Skip to content

Commit a6e2546

Browse files
committed
chore: implement PR feedback
1 parent 42a5a2d commit a6e2546

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -588,18 +588,19 @@ private static string ConvertByteArrayToString(byte[] hash)
588588
else
589589
{
590590
string relativePath;
591+
var referenceV3 = !string.IsNullOrEmpty(reference.ReferenceV3) ? reference.ReferenceV3! : string.Empty;
591592

592-
if (!string.IsNullOrEmpty(reference.ReferenceV3) && IsSubComponent(reference.ReferenceV3!))
593+
if (!string.IsNullOrEmpty(referenceV3) && IsSubComponent(referenceV3))
593594
{
594595
// Enables setting the complete JSON path for nested subschemas e.g. #/components/schemas/person/properties/address
595596
if (useExternal)
596597
{
597-
var relPathSegment = reference.ReferenceV3!.Split('#')[1];
598+
var relPathSegment = referenceV3.Split(['#'], StringSplitOptions.RemoveEmptyEntries)[1];
598599
relativePath = $"#{relPathSegment}";
599600
}
600601
else
601602
{
602-
relativePath = reference.ReferenceV3!;
603+
relativePath = referenceV3;
603604
}
604605
}
605606
else
@@ -614,7 +615,7 @@ private static string ConvertByteArrayToString(byte[] hash)
614615
: BaseUri + relativePath;
615616
}
616617

617-
if (reference.Type is ReferenceType.Schema && !uriLocation.StartsWith("http", StringComparison.OrdinalIgnoreCase))
618+
if (reference.Type is ReferenceType.Schema && uriLocation.Contains('#'))
618619
{
619620
return Workspace?.ResolveJsonSchemaReference(new Uri(uriLocation).AbsoluteUri);
620621
}
@@ -630,11 +631,11 @@ private static bool IsSubComponent(string reference)
630631

631632
if (fragment.StartsWith("/components/schemas/", StringComparison.OrdinalIgnoreCase))
632633
{
633-
var segments = fragment.Split('/');
634+
var segments = fragment.Split(['/'], StringSplitOptions.RemoveEmptyEntries);
634635

635-
// Expect exactly 4 segments for root-level schema: ["", "components", "schemas", "person"]
636+
// Expect exactly 3 segments for root-level schema: ["components", "schemas", "person"]
636637
// Anything longer means it's a subcomponent.
637-
return segments.Length > 4;
638+
return segments.Length > 3;
638639
}
639640

640641
return false;

src/Microsoft.OpenApi/Models/OpenApiReference.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ public string? ReferenceV3
106106
return Id;
107107
}
108108

109-
return _referenceV3 = "#/components/" + Type.GetDisplayName() + "/" + Id;
109+
return _referenceV3 = $"#/components/{Type.GetDisplayName()}/{Id}";
110110
}
111-
set
111+
private set
112112
{
113113
if (value is not null)
114114
{
@@ -139,7 +139,7 @@ public string? ReferenceV2
139139
return Id;
140140
}
141141

142-
return "#/" + GetReferenceTypeNameAsV2(Type) + "/" + Id;
142+
return $"#/{GetReferenceTypeNameAsV2(Type)}/{Id}";
143143
}
144144
}
145145

@@ -316,7 +316,7 @@ internal void SetSummaryAndDescriptionFromMapNode(MapNode mapNode)
316316
internal void SetJsonPointerPath(string pointer)
317317
{
318318
// Eg of an internal subcomponent's JSONPath: #/components/schemas/person/properties/address
319-
if ((pointer.Contains("#") || pointer.StartsWith("http", StringComparison.OrdinalIgnoreCase))
319+
if ((pointer.Contains('#') || pointer.StartsWith("http", StringComparison.OrdinalIgnoreCase))
320320
&& !string.IsNullOrEmpty(ReferenceV3) && !ReferenceV3!.Equals(pointer, StringComparison.OrdinalIgnoreCase))
321321
{
322322
ReferenceV3 = pointer;

src/Microsoft.OpenApi/Services/OpenApiWorkspace.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public bool Contains(string location)
336336
/// </summary>
337337
/// <param name="location"></param>
338338
/// <returns></returns>
339-
public IOpenApiSchema? ResolveJsonSchemaReference(string location)
339+
internal IOpenApiSchema? ResolveJsonSchemaReference(string location)
340340
{
341341
/* Enables resolving references for nested subschemas
342342
* Examples:
@@ -360,7 +360,7 @@ public bool Contains(string location)
360360
Fragment = fragment
361361
}; // to avoid escaping the # character in the resulting Uri
362362

363-
if (_IOpenApiReferenceableRegistry.TryGetValue(uriBuilder.Uri, out var schema) && schema is OpenApiSchema targetSchema)
363+
if (_IOpenApiReferenceableRegistry.TryGetValue(uriBuilder.Uri, out var schema) && schema is IOpenApiSchema targetSchema)
364364
{
365365
// traverse remaining segments after fetching the base schema
366366
var remainingSegments = pathSegments.Skip(4).ToArray();

test/Microsoft.OpenApi.Readers.Tests/V31Tests/ReferenceSamples/rootComponentSchemaReference.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ paths:
1616
$ref: '#/components/schemas/specialitem'
1717
components:
1818
schemas:
19-
specialitem: # Use the item type but provide a different title for the type
19+
specialitem:
2020
title: Special Item
2121
$ref: "#/components/schemas/item"
2222
item:

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ namespace Microsoft.OpenApi.Models
965965
public bool IsFragment { get; init; }
966966
public bool IsLocal { get; }
967967
public string? ReferenceV2 { get; }
968-
public string? ReferenceV3 { get; set; }
968+
public string? ReferenceV3 { get; }
969969
public string? Summary { get; set; }
970970
public Microsoft.OpenApi.Models.ReferenceType Type { get; init; }
971971
public void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { }
@@ -1650,7 +1650,6 @@ namespace Microsoft.OpenApi.Services
16501650
public System.Uri? GetDocumentId(string? key) { }
16511651
public bool RegisterComponentForDocument<T>(Microsoft.OpenApi.Models.OpenApiDocument openApiDocument, T componentToRegister, string id) { }
16521652
public void RegisterComponents(Microsoft.OpenApi.Models.OpenApiDocument document) { }
1653-
public Microsoft.OpenApi.Models.Interfaces.IOpenApiSchema? ResolveJsonSchemaReference(string location) { }
16541653
public T? ResolveReference<T>(string location) { }
16551654
}
16561655
public class OperationSearch : Microsoft.OpenApi.Services.OpenApiVisitorBase

0 commit comments

Comments
 (0)