4
4
using System ;
5
5
using Microsoft . OpenApi . Extensions ;
6
6
using Microsoft . OpenApi . Interfaces ;
7
+ using Microsoft . OpenApi . Models . Interfaces ;
7
8
using Microsoft . OpenApi . Writers ;
8
9
9
10
namespace Microsoft . OpenApi . Models
10
11
{
11
12
/// <summary>
12
13
/// A simple object to allow referencing other components in the specification, internally and externally.
13
14
/// </summary>
14
- public class OpenApiReference : IOpenApiSerializable
15
+ public class OpenApiReference : IOpenApiSerializable , IOpenApiDescribedElement , IOpenApiSummarizedElement
15
16
{
16
17
/// <summary>
17
18
/// A short summary which by default SHOULD override that of the referenced component.
@@ -32,13 +33,13 @@ public class OpenApiReference : IOpenApiSerializable
32
33
/// 1. a absolute/relative file path, for example: ../commons/pet.json
33
34
/// 2. a Url, for example: http://localhost/pet.json
34
35
/// </summary>
35
- public string ExternalResource { get ; set ; }
36
+ public string ExternalResource { get ; init ; }
36
37
37
38
/// <summary>
38
39
/// The element type referenced.
39
40
/// </summary>
40
41
/// <remarks>This must be present if <see cref="ExternalResource"/> is not present.</remarks>
41
- public ReferenceType ? Type { get ; set ; }
42
+ public ReferenceType ? Type { get ; init ; }
42
43
43
44
/// <summary>
44
45
/// The identifier of the reusable component of one particular ReferenceType.
@@ -47,7 +48,7 @@ public class OpenApiReference : IOpenApiSerializable
47
48
/// If ExternalResource is not present, this is the name of the component without the reference type name.
48
49
/// For example, if the reference is '#/components/schemas/componentName', the Id is 'componentName'.
49
50
/// </summary>
50
- public string Id { get ; set ; }
51
+ public string Id { get ; init ; }
51
52
52
53
/// <summary>
53
54
/// Gets a flag indicating whether this reference is an external reference.
@@ -62,12 +63,13 @@ public class OpenApiReference : IOpenApiSerializable
62
63
/// <summary>
63
64
/// Gets a flag indicating whether a file is a valid OpenAPI document or a fragment
64
65
/// </summary>
65
- public bool IsFragment = false ;
66
+ public bool IsFragment { get ; init ; }
66
67
68
+ private OpenApiDocument hostDocument ;
67
69
/// <summary>
68
70
/// The OpenApiDocument that is hosting the OpenApiReference instance. This is used to enable dereferencing the reference.
69
71
/// </summary>
70
- public OpenApiDocument HostDocument { get ; set ; }
72
+ public OpenApiDocument HostDocument { get => hostDocument ; init => hostDocument = value ; }
71
73
72
74
/// <summary>
73
75
/// Gets the full reference string for v3.0.
@@ -145,12 +147,13 @@ public OpenApiReference() { }
145
147
/// </summary>
146
148
public OpenApiReference ( OpenApiReference reference )
147
149
{
148
- Summary = reference ? . Summary ;
149
- Description = reference ? . Description ;
150
- ExternalResource = reference ? . ExternalResource ;
151
- Type = reference ? . Type ;
152
- Id = reference ? . Id ;
153
- HostDocument = reference ? . HostDocument ;
150
+ Utils . CheckArgumentNull ( reference ) ;
151
+ Summary = reference . Summary ;
152
+ Description = reference . Description ;
153
+ ExternalResource = reference . ExternalResource ;
154
+ Type = reference . Type ;
155
+ Id = reference . Id ;
156
+ HostDocument = reference . HostDocument ;
154
157
}
155
158
156
159
/// <summary>
@@ -276,5 +279,16 @@ private string GetReferenceTypeNameAsV2(ReferenceType type)
276
279
// to indicate that the reference is not pointing to any object.
277
280
} ;
278
281
}
282
+
283
+ /// <summary>
284
+ /// Sets the host document after deserialization or before serialization.
285
+ /// This method is internal on purpose to avoid consumers mutating the host document.
286
+ /// </summary>
287
+ /// <param name="currentDocument">Host document to set if none is present</param>
288
+ internal void EnsureHostDocumentIsSet ( OpenApiDocument currentDocument )
289
+ {
290
+ Utils . CheckArgumentNull ( currentDocument ) ;
291
+ hostDocument ??= currentDocument ;
292
+ }
279
293
}
280
294
}
0 commit comments