16
16
using Microsoft . OpenApi . Services ;
17
17
using Microsoft . OpenApi . Writers ;
18
18
19
+ #nullable enable
20
+
19
21
namespace Microsoft . OpenApi . Models
20
22
{
21
23
/// <summary>
@@ -26,60 +28,60 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible, IBaseDo
26
28
/// <summary>
27
29
/// Related workspace containing OpenApiDocuments that are referenced in this document
28
30
/// </summary>
29
- public OpenApiWorkspace Workspace { get ; set ; }
31
+ public OpenApiWorkspace ? Workspace { get ; set ; }
30
32
31
33
/// <summary>
32
34
/// REQUIRED. Provides metadata about the API. The metadata MAY be used by tooling as required.
33
35
/// </summary>
34
- public OpenApiInfo Info { get ; set ; }
36
+ public OpenApiInfo ? Info { get ; set ; }
35
37
36
38
/// <summary>
37
39
/// The default value for the $schema keyword within Schema Objects contained within this OAS document. This MUST be in the form of a URI.
38
40
/// </summary>
39
- public string JsonSchemaDialect { get ; set ; }
41
+ public string ? JsonSchemaDialect { get ; set ; }
40
42
41
43
/// <summary>
42
44
/// An array of Server Objects, which provide connectivity information to a target server.
43
45
/// </summary>
44
- public IList < OpenApiServer > Servers { get ; set ; } = new List < OpenApiServer > ( ) ;
46
+ public IList < OpenApiServer > ? Servers { get ; set ; } = new List < OpenApiServer > ( ) ;
45
47
46
48
/// <summary>
47
49
/// REQUIRED. The available paths and operations for the API.
48
50
/// </summary>
49
- public OpenApiPaths Paths { get ; set ; }
51
+ public OpenApiPaths ? Paths { get ; set ; }
50
52
51
53
/// <summary>
52
54
/// The incoming webhooks that MAY be received as part of this API and that the API consumer MAY choose to implement.
53
55
/// A map of requests initiated other than by an API call, for example by an out of band registration.
54
56
/// The key name is a unique string to refer to each webhook, while the (optionally referenced) Path Item Object describes a request that may be initiated by the API provider and the expected responses
55
57
/// </summary>
56
- public IDictionary < string , OpenApiPathItem > Webhooks { get ; set ; } = new Dictionary < string , OpenApiPathItem > ( ) ;
58
+ public IDictionary < string , OpenApiPathItem > ? Webhooks { get ; set ; } = new Dictionary < string , OpenApiPathItem > ( ) ;
57
59
58
60
/// <summary>
59
61
/// An element to hold various schemas for the specification.
60
62
/// </summary>
61
- public OpenApiComponents Components { get ; set ; }
63
+ public OpenApiComponents ? Components { get ; set ; }
62
64
63
65
/// <summary>
64
66
/// A declaration of which security mechanisms can be used across the API.
65
67
/// </summary>
66
- public IList < OpenApiSecurityRequirement > SecurityRequirements { get ; set ; } =
68
+ public IList < OpenApiSecurityRequirement > ? SecurityRequirements { get ; set ; } =
67
69
new List < OpenApiSecurityRequirement > ( ) ;
68
70
69
71
/// <summary>
70
72
/// A list of tags used by the specification with additional metadata.
71
73
/// </summary>
72
- public IList < OpenApiTag > Tags { get ; set ; } = new List < OpenApiTag > ( ) ;
74
+ public IList < OpenApiTag > ? Tags { get ; set ; } = new List < OpenApiTag > ( ) ;
73
75
74
76
/// <summary>
75
77
/// Additional external documentation.
76
78
/// </summary>
77
- public OpenApiExternalDocs ExternalDocs { get ; set ; }
79
+ public OpenApiExternalDocs ? ExternalDocs { get ; set ; }
78
80
79
81
/// <summary>
80
82
/// This object MAY be extended with Specification Extensions.
81
83
/// </summary>
82
- public IDictionary < string , IOpenApiExtension > Extensions { get ; set ; } = new Dictionary < string , IOpenApiExtension > ( ) ;
84
+ public IDictionary < string , IOpenApiExtension > ? Extensions { get ; set ; } = new Dictionary < string , IOpenApiExtension > ( ) ;
83
85
84
86
/// <summary>
85
87
/// The unique hash code of the generated OpenAPI document
@@ -97,13 +99,13 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible, IBaseDo
97
99
public OpenApiDocument ( )
98
100
{
99
101
Workspace = new OpenApiWorkspace ( ) ;
100
- BaseUri = new ( OpenApiConstants . BaseRegistryUri + Guid . NewGuid ( ) . ToString ( ) ) ;
102
+ BaseUri = new ( OpenApiConstants . BaseRegistryUri + Guid . NewGuid ( ) . ToString ( ) ) ;
101
103
}
102
104
103
105
/// <summary>
104
106
/// Initializes a copy of an an <see cref="OpenApiDocument"/> object
105
107
/// </summary>
106
- public OpenApiDocument ( OpenApiDocument document )
108
+ public OpenApiDocument ( OpenApiDocument ? document )
107
109
{
108
110
Workspace = document ? . Workspace != null ? new ( document ? . Workspace ) : null ;
109
111
Info = document ? . Info != null ? new ( document ? . Info ) : null ;
@@ -116,6 +118,7 @@ public OpenApiDocument(OpenApiDocument document)
116
118
Tags = document ? . Tags != null ? new List < OpenApiTag > ( document . Tags ) : null ;
117
119
ExternalDocs = document ? . ExternalDocs != null ? new ( document ? . ExternalDocs ) : null ;
118
120
Extensions = document ? . Extensions != null ? new Dictionary < string , IOpenApiExtension > ( document . Extensions ) : null ;
121
+ BaseUri = document ? . BaseUri != null ? document . BaseUri : new ( OpenApiConstants . BaseRegistryUri + Guid . NewGuid ( ) . ToString ( ) ) ;
119
122
}
120
123
121
124
/// <summary>
@@ -242,8 +245,10 @@ public void SerializeAsV2(IOpenApiWriter writer)
242
245
243
246
if ( loops . TryGetValue ( typeof ( JsonSchema ) , out List < object > schemas ) )
244
247
{
245
- var openApiSchemas = schemas . Cast < JsonSchema > ( ) . Distinct ( )
246
- . ToDictionary ( k => k . GetRef ( ) . ToString ( ) ) ;
248
+ var openApiSchemas = schemas . Cast < JsonSchema > ( )
249
+ . Distinct ( )
250
+ . Where ( s => s . GetRef ( ) != null )
251
+ . ToDictionary ( k => k . GetRef ( ) ! . ToString ( ) ) ;
247
252
248
253
foreach ( var schema in openApiSchemas . Values . ToList ( ) )
249
254
{
@@ -377,7 +382,7 @@ private static string ParseServerUrl(OpenApiServer server)
377
382
return parsedUrl ;
378
383
}
379
384
380
- private static void WriteHostInfoV2 ( IOpenApiWriter writer , IList < OpenApiServer > servers )
385
+ private static void WriteHostInfoV2 ( IOpenApiWriter writer , IList < OpenApiServer > ? servers )
381
386
{
382
387
if ( servers == null || ! servers . Any ( ) )
383
388
{
@@ -457,7 +462,7 @@ public void SetReferenceHostDocument()
457
462
/// <summary>
458
463
/// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="OpenApiReference"/> object
459
464
/// </summary>
460
- internal T ResolveReferenceTo < T > ( OpenApiReference reference ) where T : class , IOpenApiReferenceable
465
+ internal T ? ResolveReferenceTo < T > ( OpenApiReference reference ) where T : class , IOpenApiReferenceable
461
466
{
462
467
if ( reference . IsExternal )
463
468
{
@@ -472,7 +477,7 @@ internal T ResolveReferenceTo<T>(OpenApiReference reference) where T : class, IO
472
477
/// <summary>
473
478
/// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="OpenApiReference"/> object
474
479
/// </summary>
475
- public IOpenApiReferenceable ResolveReference ( OpenApiReference reference )
480
+ public IOpenApiReferenceable ? ResolveReference ( OpenApiReference reference )
476
481
{
477
482
return ResolveReference ( reference , false ) ;
478
483
}
@@ -482,7 +487,7 @@ public IOpenApiReferenceable ResolveReference(OpenApiReference reference)
482
487
/// </summary>
483
488
/// <param name="referenceUri"></param>
484
489
/// <returns>A JsonSchema ref.</returns>
485
- public JsonSchema ResolveJsonSchemaReference ( Uri referenceUri )
490
+ public JsonSchema ? ResolveJsonSchemaReference ( Uri referenceUri )
486
491
{
487
492
const char pound = '#' ;
488
493
string uriLocation ;
@@ -492,7 +497,7 @@ public JsonSchema ResolveJsonSchemaReference(Uri referenceUri)
492
497
{
493
498
// External reference, ex: ./TodoReference.yaml#/components/schemas/todo
494
499
string externalUri = referenceUri . OriginalString . Split ( pound ) . First ( ) ;
495
- Uri externalDocId = Workspace . GetDocumentId ( externalUri ) ;
500
+ Uri ? externalDocId = Workspace ? . GetDocumentId ( externalUri ) ;
496
501
string relativePath = referenceUri . OriginalString . Split ( pound ) . Last ( ) ;
497
502
uriLocation = externalDocId + relativePath ;
498
503
}
@@ -501,7 +506,7 @@ public JsonSchema ResolveJsonSchemaReference(Uri referenceUri)
501
506
uriLocation = BaseUri + referenceUri . ToString ( ) . TrimStart ( pound ) ;
502
507
}
503
508
504
- return ( JsonSchema ) Workspace . ResolveReference < IBaseDocument > ( uriLocation ) ;
509
+ return Workspace ? . ResolveReference < IBaseDocument > ( uriLocation ) as JsonSchema ;
505
510
}
506
511
507
512
/// <summary>
@@ -541,7 +546,7 @@ private static string ConvertByteArrayToString(byte[] hash)
541
546
/// <summary>
542
547
/// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="OpenApiReference"/> object
543
548
/// </summary>
544
- internal IOpenApiReferenceable ResolveReference ( OpenApiReference reference , bool useExternal )
549
+ internal IOpenApiReferenceable ? ResolveReference ( OpenApiReference ? reference , bool useExternal )
545
550
{
546
551
if ( reference == null )
547
552
{
@@ -556,7 +561,7 @@ internal IOpenApiReferenceable ResolveReference(OpenApiReference reference, bool
556
561
// Special case for Tag
557
562
if ( reference . Type == ReferenceType . Tag )
558
563
{
559
- foreach ( var tag in this . Tags )
564
+ foreach ( var tag in this . Tags ?? Enumerable . Empty < OpenApiTag > ( ) )
560
565
{
561
566
if ( tag . Name == reference . Id )
562
567
{
@@ -572,10 +577,10 @@ internal IOpenApiReferenceable ResolveReference(OpenApiReference reference, bool
572
577
string relativePath = OpenApiConstants . ComponentsSegment + reference . Type . GetDisplayName ( ) + "/" + reference . Id ;
573
578
574
579
uriLocation = useExternal
575
- ? Workspace . GetDocumentId ( reference . ExternalResource ) ? . OriginalString + relativePath
580
+ ? Workspace ? . GetDocumentId ( reference . ExternalResource ) ? . OriginalString + relativePath
576
581
: BaseUri + relativePath ;
577
582
578
- return Workspace . ResolveReference < IOpenApiReferenceable > ( uriLocation ) ;
583
+ return Workspace ? . ResolveReference < IOpenApiReferenceable > ( uriLocation ) ;
579
584
}
580
585
581
586
/// <summary>
@@ -584,7 +589,7 @@ internal IOpenApiReferenceable ResolveReference(OpenApiReference reference, bool
584
589
/// <param name="url"> The path to the OpenAPI file.</param>
585
590
/// <param name="settings"></param>
586
591
/// <returns></returns>
587
- public static ReadResult Load ( string url , OpenApiReaderSettings settings = null )
592
+ public static ReadResult Load ( string url , OpenApiReaderSettings ? settings = null )
588
593
{
589
594
return OpenApiModelFactory . Load ( url , settings ) ;
590
595
}
@@ -598,7 +603,7 @@ public static ReadResult Load(string url, OpenApiReaderSettings settings = null)
598
603
/// <returns></returns>
599
604
public static ReadResult Load ( Stream stream ,
600
605
string format ,
601
- OpenApiReaderSettings settings = null )
606
+ OpenApiReaderSettings ? settings = null )
602
607
{
603
608
return OpenApiModelFactory . Load ( stream , format , settings ) ;
604
609
}
@@ -612,7 +617,7 @@ public static ReadResult Load(Stream stream,
612
617
/// <returns></returns>
613
618
public static ReadResult Load ( TextReader input ,
614
619
string format ,
615
- OpenApiReaderSettings settings = null )
620
+ OpenApiReaderSettings ? settings = null )
616
621
{
617
622
return OpenApiModelFactory . Load ( input , format , settings ) ;
618
623
}
@@ -623,7 +628,7 @@ public static ReadResult Load(TextReader input,
623
628
/// <param name="url"> The path to the OpenAPI file.</param>
624
629
/// <param name="settings">The OpenApi reader settings.</param>
625
630
/// <returns></returns>
626
- public static async Task < ReadResult > LoadAsync ( string url , OpenApiReaderSettings settings = null )
631
+ public static async Task < ReadResult > LoadAsync ( string url , OpenApiReaderSettings ? settings = null )
627
632
{
628
633
return await OpenApiModelFactory . LoadAsync ( url , settings ) ;
629
634
}
@@ -636,7 +641,7 @@ public static async Task<ReadResult> LoadAsync(string url, OpenApiReaderSettings
636
641
/// <param name="settings">The OpenApi reader settings.</param>
637
642
/// <param name="cancellationToken">Propagates information about operation cancelling.</param>
638
643
/// <returns></returns>
639
- public static async Task < ReadResult > LoadAsync ( Stream stream , string format , OpenApiReaderSettings settings = null , CancellationToken cancellationToken = default )
644
+ public static async Task < ReadResult > LoadAsync ( Stream stream , string format , OpenApiReaderSettings ? settings = null , CancellationToken cancellationToken = default )
640
645
{
641
646
return await OpenApiModelFactory . LoadAsync ( stream , format , settings , cancellationToken ) ;
642
647
}
@@ -648,7 +653,7 @@ public static async Task<ReadResult> LoadAsync(Stream stream, string format, Ope
648
653
/// <param name="format"> The OpenAPI format to use during parsing.</param>
649
654
/// <param name="settings">The OpenApi reader settings.</param>
650
655
/// <returns></returns>
651
- public static async Task < ReadResult > LoadAsync ( TextReader input , string format , OpenApiReaderSettings settings = null )
656
+ public static async Task < ReadResult > LoadAsync ( TextReader input , string format , OpenApiReaderSettings ? settings = null )
652
657
{
653
658
return await OpenApiModelFactory . LoadAsync ( input , format , settings ) ;
654
659
}
@@ -661,8 +666,8 @@ public static async Task<ReadResult> LoadAsync(TextReader input, string format,
661
666
/// <param name="settings"></param>
662
667
/// <returns></returns>
663
668
public static ReadResult Parse ( string input ,
664
- string format = null ,
665
- OpenApiReaderSettings settings = null )
669
+ string ? format = null ,
670
+ OpenApiReaderSettings ? settings = null )
666
671
{
667
672
return OpenApiModelFactory . Parse ( input , format , settings ) ;
668
673
}
@@ -674,18 +679,18 @@ public static ReadResult Parse(string input,
674
679
/// <param name="options"></param>
675
680
/// <returns></returns>
676
681
/// <exception cref="NotImplementedException"></exception>
677
- public JsonSchema FindSubschema ( Json . Pointer . JsonPointer pointer , EvaluationOptions options )
682
+ public JsonSchema ? FindSubschema ( Json . Pointer . JsonPointer pointer , EvaluationOptions options )
678
683
{
679
684
var locationUri = string . Concat ( BaseUri , pointer ) ;
680
- return ( JsonSchema ) Workspace . ResolveReference < IBaseDocument > ( locationUri ) ;
685
+ return Workspace ? . ResolveReference < IBaseDocument > ( locationUri ) as JsonSchema ;
681
686
}
682
687
}
683
688
684
689
internal class FindSchemaReferences : OpenApiVisitorBase
685
690
{
686
- private Dictionary < string , JsonSchema > Schemas ;
691
+ private Dictionary < string , JsonSchema > ? Schemas ;
687
692
688
- public static void ResolveSchemas ( OpenApiComponents components , Dictionary < string , JsonSchema > schemas )
693
+ public static void ResolveSchemas ( OpenApiComponents ? components , Dictionary < string , JsonSchema > schemas )
689
694
{
690
695
var visitor = new FindSchemaReferences ( ) ;
691
696
visitor . Schemas = schemas ;
0 commit comments