Skip to content

Commit 3df5c98

Browse files
committed
chore: remove deprecated code and tests; make param required
1 parent 1b793a4 commit 3df5c98

File tree

9 files changed

+13
-589
lines changed

9 files changed

+13
-589
lines changed

src/Microsoft.OpenApi/Interfaces/IOpenApiVersionService.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ namespace Microsoft.OpenApi.Interfaces
1111
/// </summary>
1212
internal interface IOpenApiVersionService
1313
{
14-
/// <summary>
15-
/// Parse the string to a <see cref="OpenApiReference"/> object.
16-
/// </summary>
17-
/// <param name="reference">The reference string.</param>
18-
/// <param name="type">The type of the reference.</param>
19-
/// <param name="summary">The summary of the reference.</param>
20-
/// <param name="description">A reference description</param>
21-
/// <returns>The <see cref="OpenApiReference"/> object or null.</returns>
22-
OpenApiReference? ConvertToOpenApiReference(string reference, ReferenceType? type, string? summary = null, string? description = null);
23-
2414
/// <summary>
2515
/// Loads an OpenAPI Element from a document fragment
2616
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,6 @@ private static string ConvertByteArrayToString(byte[] hash)
516516
return null;
517517
}
518518

519-
if (!reference.Type.HasValue)
520-
{
521-
throw new ArgumentException(Properties.SRResource.LocalReferenceRequiresType);
522-
}
523-
524519
string uriLocation;
525520
var id = reference.Id;
526521
if (!string.IsNullOrEmpty(id) && id!.Contains("/")) // this means its a URL reference

src/Microsoft.OpenApi/Models/OpenApiReference.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class OpenApiReference : IOpenApiSerializable, IOpenApiDescribedElement,
3939
/// The element type referenced.
4040
/// </summary>
4141
/// <remarks>This must be present if <see cref="ExternalResource"/> is not present.</remarks>
42-
public ReferenceType? Type { get; init; }
42+
public ReferenceType Type { get; init; }
4343

4444
/// <summary>
4545
/// The identifier of the reusable component of one particular ReferenceType.
@@ -83,11 +83,6 @@ public string? ReferenceV3
8383
return GetExternalReferenceV3();
8484
}
8585

86-
if (!Type.HasValue)
87-
{
88-
throw new ArgumentNullException(nameof(Type));
89-
}
90-
9186
if (Type == ReferenceType.Tag)
9287
{
9388
return Id;
@@ -102,7 +97,7 @@ public string? ReferenceV3
10297
return Id;
10398
}
10499

105-
return "#/components/" + Type.Value.GetDisplayName() + "/" + Id;
100+
return "#/components/" + Type.GetDisplayName() + "/" + Id;
106101
}
107102
}
108103

@@ -118,11 +113,6 @@ public string? ReferenceV2
118113
return GetExternalReferenceV2();
119114
}
120115

121-
if (!Type.HasValue)
122-
{
123-
throw new ArgumentNullException(nameof(Type));
124-
}
125-
126116
if (Type == ReferenceType.Tag)
127117
{
128118
return Id;
@@ -133,7 +123,7 @@ public string? ReferenceV2
133123
return Id;
134124
}
135125

136-
return "#/" + GetReferenceTypeNameAsV2(Type.Value) + "/" + Id;
126+
return "#/" + GetReferenceTypeNameAsV2(Type) + "/" + Id;
137127
}
138128
}
139129

@@ -246,18 +236,15 @@ public void SerializeAsV2(IOpenApiWriter writer)
246236
return Id;
247237
}
248238

249-
if (Type.HasValue)
250-
{
251-
return ExternalResource + "#/components/" + Type.Value.GetDisplayName() + "/"+ Id;
252-
}
239+
return ExternalResource + "#/components/" + Type.GetDisplayName() + "/"+ Id;
253240
}
254241

255242
return ExternalResource;
256243
}
257244

258245
private string? GetExternalReferenceV2()
259246
{
260-
if (Id is not null && Type is not null)
247+
if (Id is not null)
261248
{
262249
return ExternalResource + "#/" + GetReferenceTypeNameAsV2((ReferenceType)Type) + "/" + Id;
263250
}

src/Microsoft.OpenApi/Reader/V2/OpenApiV2VersionService.cs

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -129,86 +129,6 @@ private static ReferenceType GetReferenceTypeV2FromName(string referenceType)
129129
}
130130
}
131131

132-
/// <summary>
133-
/// Parse the string to a <see cref="OpenApiReference"/> object.
134-
/// </summary>
135-
public OpenApiReference? ConvertToOpenApiReference(string reference, ReferenceType? type, string? summary = null, string? description = null)
136-
{
137-
if (!string.IsNullOrWhiteSpace(reference))
138-
{
139-
var segments = reference.Split('#');
140-
if (segments.Length == 1)
141-
{
142-
// Either this is an external reference as an entire file
143-
// or a simple string-style reference for tag and security scheme.
144-
if (type == null)
145-
{
146-
// "$ref": "Pet.json"
147-
return new()
148-
{
149-
ExternalResource = segments[0]
150-
};
151-
}
152-
153-
if (type is ReferenceType.Tag or ReferenceType.SecurityScheme)
154-
{
155-
return new()
156-
{
157-
Type = type,
158-
Id = reference
159-
};
160-
}
161-
}
162-
else if (segments.Length == 2)
163-
{
164-
if (reference.StartsWith("#", StringComparison.OrdinalIgnoreCase))
165-
{
166-
// "$ref": "#/definitions/Pet"
167-
try
168-
{
169-
return ParseLocalReference(segments[1]);
170-
}
171-
catch (OpenApiException ex)
172-
{
173-
Diagnostic.Errors.Add(new(ex));
174-
return null;
175-
}
176-
}
177-
178-
// Where fragments point into a non-OpenAPI document, the id will be the complete fragment identifier
179-
var id = segments[1];
180-
// $ref: externalSource.yaml#/Pet
181-
if (id.StartsWith("/definitions/", StringComparison.Ordinal))
182-
{
183-
var localSegments = id.Split('/');
184-
var referencedType = GetReferenceTypeV2FromName(localSegments[1]);
185-
if (type == null)
186-
{
187-
type = referencedType;
188-
}
189-
else
190-
{
191-
if (type != referencedType)
192-
{
193-
throw new OpenApiException("Referenced type mismatch");
194-
}
195-
}
196-
id = localSegments[2];
197-
}
198-
199-
// $ref: externalSource.yaml#/Pet
200-
return new()
201-
{
202-
ExternalResource = segments[0],
203-
Type = type,
204-
Id = id
205-
};
206-
}
207-
}
208-
209-
throw new OpenApiException(string.Format(SRResource.ReferenceHasInvalidFormat, reference));
210-
}
211-
212132
public OpenApiDocument LoadDocument(RootNode rootNode)
213133
{
214134
return OpenApiV2Deserializer.LoadOpenApi(rootNode);

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

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -65,112 +65,6 @@ public OpenApiV3VersionService(OpenApiDiagnostic diagnostic)
6565
[typeof(OpenApiXml)] = OpenApiV3Deserializer.LoadXml
6666
};
6767

68-
/// <summary>
69-
/// Parse the string to a <see cref="OpenApiReference"/> object.
70-
/// </summary>
71-
/// <param name="reference">The URL of the reference</param>
72-
/// <param name="type">The type of object referenced based on the context of the reference</param>
73-
/// <param name="summary"></param>
74-
/// <param name="description"></param>
75-
public OpenApiReference? ConvertToOpenApiReference(
76-
string reference,
77-
ReferenceType? type,
78-
string? summary = null,
79-
string? description = null)
80-
{
81-
if (!string.IsNullOrWhiteSpace(reference))
82-
{
83-
var segments = reference.Split('#');
84-
if (segments.Length == 1)
85-
{
86-
if (type is ReferenceType.Tag or ReferenceType.SecurityScheme)
87-
{
88-
return new()
89-
{
90-
Type = type,
91-
Id = reference
92-
};
93-
}
94-
95-
// Either this is an external reference as an entire file
96-
// or a simple string-style reference for tag and security scheme.
97-
return new()
98-
{
99-
Type = type,
100-
ExternalResource = segments[0]
101-
};
102-
}
103-
else if (segments.Length == 2)
104-
{
105-
if (reference.StartsWith("#", StringComparison.OrdinalIgnoreCase))
106-
{
107-
// "$ref": "#/components/schemas/Pet"
108-
try
109-
{
110-
return ParseLocalReference(segments[1]);
111-
}
112-
catch (OpenApiException ex)
113-
{
114-
Diagnostic.Errors.Add(new(ex));
115-
return null;
116-
}
117-
}
118-
// Where fragments point into a non-OpenAPI document, the id will be the complete fragment identifier
119-
var id = segments[1];
120-
var isFragment = false;
121-
122-
// $ref: externalSource.yaml#/Pet
123-
if (id.StartsWith("/components/", StringComparison.Ordinal))
124-
{
125-
var localSegments = segments[1].Split('/');
126-
localSegments[2].TryGetEnumFromDisplayName<ReferenceType>(out var referencedType);
127-
if (type == null)
128-
{
129-
type = referencedType;
130-
}
131-
else
132-
{
133-
if (type != referencedType)
134-
{
135-
throw new OpenApiException("Referenced type mismatch");
136-
}
137-
}
138-
id = localSegments[3];
139-
}
140-
else if (id.StartsWith("/paths/", StringComparison.Ordinal))
141-
{
142-
var localSegments = segments[1].Split(_pathSeparator, StringSplitOptions.RemoveEmptyEntries);
143-
if (localSegments.Length == 2)
144-
{
145-
// The reference of a path may contain JSON escape character ~1 for the forward-slash character, replace this otherwise
146-
// the reference cannot be resolved.
147-
id = localSegments[1].Replace("~1", "/");
148-
}
149-
else
150-
{
151-
throw new OpenApiException("Referenced Path mismatch");
152-
}
153-
}
154-
else
155-
{
156-
isFragment = true;
157-
}
158-
159-
var openApiReference = new OpenApiReference
160-
{
161-
ExternalResource = segments[0],
162-
Type = type,
163-
Id = id,
164-
IsFragment = isFragment,
165-
};
166-
167-
return openApiReference;
168-
}
169-
}
170-
171-
throw new OpenApiException(string.Format(SRResource.ReferenceHasInvalidFormat, reference));
172-
}
173-
17468
public OpenApiDocument LoadDocument(RootNode rootNode)
17569
{
17670
return OpenApiV3Deserializer.LoadOpenApi(rootNode);

src/Microsoft.OpenApi/Reader/V31/OpenApiV31VersionService.cs

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -64,95 +64,6 @@ public OpenApiV31VersionService(OpenApiDiagnostic diagnostic)
6464
[typeof(OpenApiXml)] = OpenApiV31Deserializer.LoadXml
6565
};
6666

67-
/// <summary>
68-
/// Parse the string to a <see cref="OpenApiReference"/> object.
69-
/// </summary>
70-
/// <param name="reference">The URL of the reference</param>
71-
/// <param name="type">The type of object refefenced based on the context of the reference</param>
72-
/// <param name="summary">The summary of the reference</param>
73-
/// <param name="description">A reference description</param>
74-
public OpenApiReference? ConvertToOpenApiReference(
75-
string reference,
76-
ReferenceType? type,
77-
string? summary = null,
78-
string? description = null)
79-
{
80-
if (!string.IsNullOrWhiteSpace(reference))
81-
{
82-
var segments = reference.Split('#');
83-
if (segments.Length == 1)
84-
{
85-
if (type == ReferenceType.Tag || type == ReferenceType.SecurityScheme)
86-
{
87-
return new OpenApiReference
88-
{
89-
Summary = summary,
90-
Description = description,
91-
Type = type,
92-
Id = reference
93-
};
94-
}
95-
96-
// Either this is an external reference as an entire file
97-
// or a simple string-style reference for tag and security scheme.
98-
return new OpenApiReference
99-
{
100-
Summary = summary,
101-
Description = description,
102-
Type = type,
103-
ExternalResource = segments[0]
104-
};
105-
}
106-
else if (segments.Length == 2)
107-
{
108-
if (reference.StartsWith("#", StringComparison.OrdinalIgnoreCase))
109-
{
110-
// "$ref": "#/components/schemas/Pet"
111-
try
112-
{
113-
return ParseLocalReference(segments[1], summary, description);
114-
}
115-
catch (OpenApiException ex)
116-
{
117-
Diagnostic.Errors.Add(new OpenApiError(ex));
118-
return null;
119-
}
120-
}
121-
// Where fragments point into a non-OpenAPI document, the id will be the complete fragment identifier
122-
string id = segments[1];
123-
// $ref: externalSource.yaml#/Pet
124-
if (id.StartsWith("/components/", StringComparison.Ordinal))
125-
{
126-
var localSegments = segments[1].Split('/');
127-
localSegments[2].TryGetEnumFromDisplayName<ReferenceType>(out var referencedType);
128-
if (type == null)
129-
{
130-
type = referencedType;
131-
}
132-
else
133-
{
134-
if (type != referencedType)
135-
{
136-
throw new OpenApiException("Referenced type mismatch");
137-
}
138-
}
139-
id = localSegments[3];
140-
}
141-
142-
return new OpenApiReference
143-
{
144-
Summary = summary,
145-
Description = description,
146-
ExternalResource = segments[0],
147-
Type = type,
148-
Id = id
149-
};
150-
}
151-
}
152-
153-
throw new OpenApiException(string.Format(SRResource.ReferenceHasInvalidFormat, reference));
154-
}
155-
15667
public OpenApiDocument LoadDocument(RootNode rootNode)
15768
{
15869
return OpenApiV31Deserializer.LoadOpenApi(rootNode);

0 commit comments

Comments
 (0)