Skip to content

Commit 398f1a2

Browse files
Merge pull request #973 from microsoft/mk/fix-null-reference-exception
Adds defensive programming
2 parents c564a98 + 17dfe3c commit 398f1a2

28 files changed

+218
-218
lines changed

src/Microsoft.OpenApi/Models/OpenApiCallback.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ public OpenApiCallback() { }
4545
/// </summary>
4646
public OpenApiCallback(OpenApiCallback callback)
4747
{
48-
PathItems = new(callback.PathItems);
49-
UnresolvedReference = callback.UnresolvedReference;
50-
Reference = new(callback.Reference);
51-
Extensions = new Dictionary<string, IOpenApiExtension>(callback.Extensions);
48+
PathItems = callback?.PathItems != null ? new(callback?.PathItems) : null;
49+
UnresolvedReference = callback?.UnresolvedReference ?? UnresolvedReference;
50+
Reference = callback?.Reference != null ? new(callback?.Reference) : null;
51+
Extensions = callback?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(callback.Extensions) : null;
5252
}
5353

5454
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiComponents.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ public OpenApiComponents() { }
7878
/// </summary>
7979
public OpenApiComponents(OpenApiComponents components)
8080
{
81-
Schemas = new Dictionary<string, OpenApiSchema>(components.Schemas);
82-
Responses = new Dictionary<string, OpenApiResponse>(components.Responses);
83-
Parameters = new Dictionary<string, OpenApiParameter>(components.Parameters);
84-
Examples = new Dictionary<string, OpenApiExample>(components.Examples);
85-
RequestBodies = new Dictionary<string, OpenApiRequestBody>(components.RequestBodies);
86-
Headers = new Dictionary<string, OpenApiHeader>(components.Headers);
87-
SecuritySchemes = new Dictionary<string, OpenApiSecurityScheme>(components.SecuritySchemes);
88-
Links = new Dictionary<string, OpenApiLink>(components.Links);
89-
Callbacks = new Dictionary<string, OpenApiCallback>(components.Callbacks);
90-
Extensions = new Dictionary<string, IOpenApiExtension>(components.Extensions);
81+
Schemas = components?.Schemas != null ? new Dictionary<string, OpenApiSchema>(components.Schemas) : null;
82+
Responses = components?.Responses != null ? new Dictionary<string, OpenApiResponse>(components.Responses) : null;
83+
Parameters = components?.Parameters != null ? new Dictionary<string, OpenApiParameter>(components.Parameters) : null;
84+
Examples = components?.Examples != null ? new Dictionary<string, OpenApiExample>(components.Examples) : null;
85+
RequestBodies = components?.RequestBodies != null ? new Dictionary<string, OpenApiRequestBody>(components.RequestBodies) : null;
86+
Headers = components?.Headers != null ? new Dictionary<string, OpenApiHeader>(components.Headers) : null;
87+
SecuritySchemes = components?.SecuritySchemes != null ? new Dictionary<string, OpenApiSecurityScheme>(components.SecuritySchemes) : null;
88+
Links = components?.Links != null ? new Dictionary<string, OpenApiLink>(components.Links) : null;
89+
Callbacks = components?.Callbacks != null ? new Dictionary<string, OpenApiCallback>(components.Callbacks) : null;
90+
Extensions = components?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(components.Extensions) : null;
9191
}
9292

9393
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiContact.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ public OpenApiContact() { }
4545
/// </summary>
4646
public OpenApiContact(OpenApiContact contact)
4747
{
48-
Name = contact.Name;
49-
Url = new Uri(contact.Url.OriginalString);
50-
Email = contact.Email;
51-
Extensions = new Dictionary<string, IOpenApiExtension>(contact.Extensions);
48+
Name = contact?.Name ?? Name;
49+
Url = contact?.Url != null ? new Uri(contact.Url.OriginalString) : null;
50+
Email = contact?.Email ?? Email;
51+
Extensions = contact?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(contact.Extensions) : null;
5252
}
5353

5454
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public OpenApiDiscriminator() { }
3232
/// </summary>
3333
public OpenApiDiscriminator(OpenApiDiscriminator discriminator)
3434
{
35-
PropertyName = discriminator.PropertyName;
36-
Mapping = new Dictionary<string, string>(discriminator.Mapping);
35+
PropertyName = discriminator?.PropertyName ?? PropertyName;
36+
Mapping = discriminator?.Mapping != null ? new Dictionary<string, string>(discriminator.Mapping) : null;
3737
}
3838

3939
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ public OpenApiDocument() {}
7272
/// </summary>
7373
public OpenApiDocument(OpenApiDocument document)
7474
{
75-
Workspace = new(document.Workspace);
76-
Info = new(document.Info);
77-
Servers = new List<OpenApiServer>(document.Servers);
78-
Paths = new(document.Paths);
79-
Components = new(document.Components);
80-
SecurityRequirements = new List<OpenApiSecurityRequirement>(document.SecurityRequirements);
81-
Tags = new List<OpenApiTag>(document.Tags);
82-
ExternalDocs = new(document.ExternalDocs);
83-
Extensions = new Dictionary<string, IOpenApiExtension>(document.Extensions);
75+
Workspace = document?.Workspace != null ? new(document?.Workspace) : null;
76+
Info = document?.Info != null ? new(document?.Info) : null;
77+
Servers = document?.Servers != null ? new List<OpenApiServer>(document.Servers) : null;
78+
Paths = document?.Paths != null ? new(document?.Paths) : null;
79+
Components = document?.Components != null ? new(document?.Components) : null;
80+
SecurityRequirements = document?.SecurityRequirements != null ? new List<OpenApiSecurityRequirement>(document.SecurityRequirements) : null;
81+
Tags = document?.Tags != null ? new List<OpenApiTag>(document.Tags) : null;
82+
ExternalDocs = document?.ExternalDocs != null ? new(document?.ExternalDocs) : null;
83+
Extensions = document?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(document.Extensions) : null;
8484
}
8585

8686
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiEncoding.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public OpenApiEncoding() {}
6363
/// </summary>
6464
public OpenApiEncoding(OpenApiEncoding encoding)
6565
{
66-
ContentType = encoding.ContentType;
67-
Headers = new Dictionary<string, OpenApiHeader>(encoding.Headers);
68-
Style = encoding.Style;
69-
Explode = encoding.Explode;
70-
AllowReserved = encoding.AllowReserved;
71-
Extensions = new Dictionary<string, IOpenApiExtension>(encoding.Extensions);
66+
ContentType = encoding?.ContentType ?? ContentType;
67+
Headers = encoding?.Headers != null ? new Dictionary<string, OpenApiHeader>(encoding.Headers) : null;
68+
Style = encoding?.Style ?? Style;
69+
Explode = encoding?.Explode ?? Explode;
70+
AllowReserved = encoding?.AllowReserved ?? AllowReserved;
71+
Extensions = encoding?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(encoding.Extensions) : null;
7272
}
7373

7474
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiExample.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ public OpenApiExample() {}
6464
/// </summary>
6565
public OpenApiExample(OpenApiExample example)
6666
{
67-
Summary = example.Summary;
68-
Description = example.Description;
69-
Value = OpenApiAnyCloneHelper.CloneFromCopyConstructor(example.Value);
70-
ExternalValue = example.ExternalValue;
71-
Extensions = new Dictionary<string, IOpenApiExtension>(example.Extensions);
72-
Reference = new(example.Reference);
73-
UnresolvedReference = example.UnresolvedReference;
67+
Summary = example?.Summary ?? Summary;
68+
Description = example?.Description ?? Description;
69+
Value = OpenApiAnyCloneHelper.CloneFromCopyConstructor(example?.Value);
70+
ExternalValue = example?.ExternalValue ?? ExternalValue;
71+
Extensions = example?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(example.Extensions) : null;
72+
Reference = example?.Reference != null ? new(example?.Reference) : null;
73+
UnresolvedReference = example?.UnresolvedReference ?? UnresolvedReference;
7474
}
7575

7676
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiExternalDocs.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public OpenApiExternalDocs() {}
3939
/// </summary>
4040
public OpenApiExternalDocs(OpenApiExternalDocs externalDocs)
4141
{
42-
Description = externalDocs.Description;
43-
Url = new Uri(externalDocs.Url.OriginalString);
44-
Extensions = new Dictionary<string, IOpenApiExtension>(externalDocs.Extensions);
42+
Description = externalDocs?.Description ?? Description;
43+
Url = externalDocs?.Url != null ? new Uri(externalDocs.Url.OriginalString) : null;
44+
Extensions = externalDocs?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(externalDocs.Extensions) : null;
4545
}
4646

4747
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiHeader.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,20 @@ public OpenApiHeader() {}
9696
/// </summary>
9797
public OpenApiHeader(OpenApiHeader header)
9898
{
99-
UnresolvedReference = header.UnresolvedReference;
100-
Reference = new(header.Reference);
101-
Description = header.Description;
102-
Required = header.Required;
103-
Deprecated = header.Deprecated;
104-
AllowEmptyValue = header.AllowEmptyValue;
105-
Style = header.Style;
106-
Explode = header.Explode;
107-
AllowReserved = header.AllowReserved;
108-
Schema = new(header.Schema);
109-
Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(header.Example);
110-
Examples = new Dictionary<string, OpenApiExample>(header.Examples);
111-
Content = new Dictionary<string, OpenApiMediaType>(header.Content);
112-
Extensions = new Dictionary<string, IOpenApiExtension>(header.Extensions);
99+
UnresolvedReference = header?.UnresolvedReference ?? UnresolvedReference;
100+
Reference = header?.Reference != null ? new(header?.Reference) : null;
101+
Description = header?.Description ?? Description;
102+
Required = header?.Required ?? Required;
103+
Deprecated = header?.Deprecated ?? Deprecated;
104+
AllowEmptyValue = header?.AllowEmptyValue ?? AllowEmptyValue;
105+
Style = header?.Style ?? Style;
106+
Explode = header?.Explode ?? Explode;
107+
AllowReserved = header?.AllowReserved ?? AllowReserved;
108+
Schema = header?.Schema != null ? new(header?.Schema) : null;
109+
Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(header?.Example);
110+
Examples = header?.Examples != null ? new Dictionary<string, OpenApiExample>(header.Examples) : null;
111+
Content = header?.Content != null ? new Dictionary<string, OpenApiMediaType>(header.Content) : null;
112+
Extensions = header?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(header.Extensions) : null;
113113
}
114114

115115
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiInfo.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ public OpenApiInfo() {}
5959
/// </summary>
6060
public OpenApiInfo(OpenApiInfo info)
6161
{
62-
Title = info.Title;
63-
Description = info.Description;
64-
Version = info.Version;
65-
TermsOfService = info.TermsOfService;
66-
Contact = new(info.Contact);
67-
License = new(info.License);
68-
Extensions = new Dictionary<string, IOpenApiExtension>(info.Extensions);
62+
Title = info?.Title ?? Title;
63+
Description = info?.Description ?? Description;
64+
Version = info?.Version ?? Version;
65+
TermsOfService = info?.TermsOfService ?? TermsOfService;
66+
Contact = info?.Contact != null ? new(info?.Contact) : null;
67+
License = info?.License != null ? new(info?.License) : null;
68+
Extensions = info?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(info.Extensions) : null;
6969
}
7070

7171
/// <summary>

0 commit comments

Comments
 (0)