2
2
// Licensed under the MIT license.
3
3
4
4
using System ;
5
+ using System . Collections . Generic ;
5
6
using System . Linq ;
6
7
using System . Text . Json . Nodes ;
7
8
using Microsoft . OpenApi . Reader ;
@@ -26,6 +27,42 @@ public class OpenApiReference : IOpenApiSerializable, IOpenApiDescribedElement,
26
27
/// </summary>
27
28
public string ? Description { get ; set ; }
28
29
30
+ /// <summary>
31
+ /// A default value which by default SHOULD override that of the referenced component.
32
+ /// If the referenced object-type does not allow a default field, then this field has no effect.
33
+ /// </summary>
34
+ public JsonNode ? Default { get ; set ; }
35
+
36
+ /// <summary>
37
+ /// A title which by default SHOULD override that of the referenced component.
38
+ /// If the referenced object-type does not allow a title field, then this field has no effect.
39
+ /// </summary>
40
+ public string ? Title { get ; set ; }
41
+
42
+ /// <summary>
43
+ /// Indicates whether the referenced component is deprecated.
44
+ /// If the referenced object-type does not allow a deprecated field, then this field has no effect.
45
+ /// </summary>
46
+ public bool ? Deprecated { get ; set ; }
47
+
48
+ /// <summary>
49
+ /// Indicates whether the referenced component is read-only.
50
+ /// If the referenced object-type does not allow a readOnly field, then this field has no effect.
51
+ /// </summary>
52
+ public bool ? ReadOnly { get ; set ; }
53
+
54
+ /// <summary>
55
+ /// Indicates whether the referenced component is write-only.
56
+ /// If the referenced object-type does not allow a writeOnly field, then this field has no effect.
57
+ /// </summary>
58
+ public bool ? WriteOnly { get ; set ; }
59
+
60
+ /// <summary>
61
+ /// Example values which by default SHOULD override those of the referenced component.
62
+ /// If the referenced object-type does not allow examples, then this field has no effect.
63
+ /// </summary>
64
+ public IList < JsonNode > ? Examples { get ; set ; }
65
+
29
66
/// <summary>
30
67
/// External resource in the reference.
31
68
/// It maybe:
@@ -153,6 +190,12 @@ public OpenApiReference(OpenApiReference reference)
153
190
Utils . CheckArgumentNull ( reference ) ;
154
191
Summary = reference . Summary ;
155
192
Description = reference . Description ;
193
+ Default = reference . Default ;
194
+ Title = reference . Title ;
195
+ Deprecated = reference . Deprecated ;
196
+ ReadOnly = reference . ReadOnly ;
197
+ WriteOnly = reference . WriteOnly ;
198
+ Examples = reference . Examples ;
156
199
ExternalResource = reference . ExternalResource ;
157
200
Type = reference . Type ;
158
201
Id = reference . Id ;
@@ -169,6 +212,17 @@ public void SerializeAsV31(IOpenApiWriter writer)
169
212
// summary and description are in 3.1 but not in 3.0
170
213
w . WriteProperty ( OpenApiConstants . Summary , Summary ) ;
171
214
w . WriteProperty ( OpenApiConstants . Description , Description ) ;
215
+
216
+ // Additional schema metadata annotations in 3.1
217
+ w . WriteOptionalObject ( OpenApiConstants . Default , Default , ( w , d ) => w . WriteAny ( d ) ) ;
218
+ w . WriteProperty ( OpenApiConstants . Title , Title ) ;
219
+ w . WriteProperty ( OpenApiConstants . Deprecated , Deprecated , false ) ;
220
+ w . WriteProperty ( OpenApiConstants . ReadOnly , ReadOnly , false ) ;
221
+ w . WriteProperty ( OpenApiConstants . WriteOnly , WriteOnly , false ) ;
222
+ if ( Examples != null && Examples . Any ( ) )
223
+ {
224
+ w . WriteOptionalCollection ( OpenApiConstants . Examples , Examples , ( w , e ) => w . WriteAny ( e ) ) ;
225
+ }
172
226
} ) ;
173
227
}
174
228
@@ -293,13 +347,15 @@ internal void EnsureHostDocumentIsSet(OpenApiDocument currentDocument)
293
347
}
294
348
private static string ? GetPropertyValueFromNode ( JsonObject jsonObject , string key ) =>
295
349
jsonObject . TryGetPropertyValue ( key , out var valueNode ) && valueNode is JsonValue valueCast && valueCast . TryGetValue < string > ( out var strValue ) ? strValue : null ;
296
- internal void SetSummaryAndDescriptionFromMapNode ( MapNode mapNode )
350
+ internal void SetMetadataFromMapNode ( MapNode mapNode )
297
351
{
298
- var ( description , summary ) = mapNode . JsonNode switch {
299
- JsonObject jsonObject => ( GetPropertyValueFromNode ( jsonObject , OpenApiConstants . Description ) ,
300
- GetPropertyValueFromNode ( jsonObject , OpenApiConstants . Summary ) ) ,
301
- _ => ( null , null )
302
- } ;
352
+ if ( mapNode . JsonNode is not JsonObject jsonObject ) return ;
353
+
354
+ // Summary and Description
355
+ var description = GetPropertyValueFromNode ( jsonObject , OpenApiConstants . Description ) ;
356
+ var summary = GetPropertyValueFromNode ( jsonObject , OpenApiConstants . Summary ) ;
357
+ var title = GetPropertyValueFromNode ( jsonObject , OpenApiConstants . Title ) ;
358
+
303
359
if ( ! string . IsNullOrEmpty ( description ) )
304
360
{
305
361
Description = description ;
@@ -308,6 +364,54 @@ internal void SetSummaryAndDescriptionFromMapNode(MapNode mapNode)
308
364
{
309
365
Summary = summary ;
310
366
}
367
+ if ( ! string . IsNullOrEmpty ( title ) )
368
+ {
369
+ Title = title ;
370
+ }
371
+
372
+ // Boolean properties
373
+ if ( jsonObject . TryGetPropertyValue ( OpenApiConstants . Deprecated , out var deprecatedNode ) && deprecatedNode is JsonValue deprecatedValue )
374
+ {
375
+ if ( deprecatedValue . TryGetValue < bool > ( out var deprecated ) )
376
+ {
377
+ Deprecated = deprecated ;
378
+ }
379
+ }
380
+
381
+ if ( jsonObject . TryGetPropertyValue ( OpenApiConstants . ReadOnly , out var readOnlyNode ) && readOnlyNode is JsonValue readOnlyValue )
382
+ {
383
+ if ( readOnlyValue . TryGetValue < bool > ( out var readOnly ) )
384
+ {
385
+ ReadOnly = readOnly ;
386
+ }
387
+ }
388
+
389
+ if ( jsonObject . TryGetPropertyValue ( OpenApiConstants . WriteOnly , out var writeOnlyNode ) && writeOnlyNode is JsonValue writeOnlyValue )
390
+ {
391
+ if ( writeOnlyValue . TryGetValue < bool > ( out var writeOnly ) )
392
+ {
393
+ WriteOnly = writeOnly ;
394
+ }
395
+ }
396
+
397
+ // Default value
398
+ if ( jsonObject . TryGetPropertyValue ( OpenApiConstants . Default , out var defaultNode ) )
399
+ {
400
+ Default = defaultNode ;
401
+ }
402
+
403
+ // Examples
404
+ if ( jsonObject . TryGetPropertyValue ( OpenApiConstants . Examples , out var examplesNode ) && examplesNode is JsonArray examplesArray )
405
+ {
406
+ Examples = new List < JsonNode > ( ) ;
407
+ foreach ( var example in examplesArray )
408
+ {
409
+ if ( example != null )
410
+ {
411
+ Examples . Add ( example ) ;
412
+ }
413
+ }
414
+ }
311
415
}
312
416
313
417
internal void SetJsonPointerPath ( string pointer , string nodeLocation )
0 commit comments