diff --git a/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj b/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj
index c8af22cb3..b75e7009c 100644
--- a/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj
+++ b/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj
@@ -3,7 +3,7 @@
netstandard2.0
latest
true
- 1.6.17
+ 1.6.18
OpenAPI.NET Readers for JSON and YAML documents
true
diff --git a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
index b2f09236f..2a674f542 100644
--- a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
+++ b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
@@ -3,7 +3,7 @@
netstandard2.0
Latest
true
- 1.6.17
+ 1.6.18
.NET models with JSON and YAML writers for OpenAPI specification
true
diff --git a/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs b/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs
index 3816fb14f..1ee1ce176 100644
--- a/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs
+++ b/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs
@@ -77,7 +77,10 @@ public void SerializeAsV3(IOpenApiWriter writer)
writer.WriteOptionalObject(OpenApiConstants.Example, Example, (w, e) => w.WriteAny(e));
// examples
- SerializeExamples(writer, Examples);
+ if (Examples != null && Examples.Any())
+ {
+ SerializeExamples(writer, Examples);
+ }
// encoding
writer.WriteOptionalMap(OpenApiConstants.Encoding, Encoding, (w, e) => e.SerializeAsV3(w));
diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs
index 168ec3ade..c9807f5a2 100644
--- a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs
+++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
+using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
@@ -1670,27 +1671,6 @@ private static OpenApiDocument ParseInputFile(string filePath)
return openApiDoc;
}
- //[Fact]
- //public void CopyConstructorForAdvancedDocumentWorks()
- //{
- // // Arrange & Act
- // var doc = new OpenApiDocument(AdvancedDocument);
-
- // var docOpId = doc.Paths["/pets"].Operations[OperationType.Get].OperationId = "findAllMyPets";
- // var advancedDocOpId = AdvancedDocument.Paths["/pets"].Operations[OperationType.Get].OperationId;
- // var responseSchemaTypeCopy = doc.Paths["/pets"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema.Type = "object";
- // var advancedDocResponseSchemaType = AdvancedDocument.Paths["/pets"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema.Type;
-
- // // Assert
- // Assert.NotNull(doc.Info);
- // Assert.NotNull(doc.Servers);
- // Assert.NotNull(doc.Paths);
- // Assert.Equal(2, doc.Paths.Count);
- // Assert.NotNull(doc.Components);
- // Assert.NotEqual(docOpId, advancedDocOpId);
- // Assert.NotEqual(responseSchemaTypeCopy, advancedDocResponseSchemaType);
- //}
-
[Fact]
public void SerializeV2DocumentWithNonArraySchemaTypeDoesNotWriteOutCollectionFormat()
{
@@ -1858,5 +1838,38 @@ public void OpenApiDocumentCopyConstructorWithAnnotationsSucceeds()
Assert.NotEqual(baseDocument.Annotations["key1"], actualDocument.Annotations["key1"]);
}
- }
+
+ [Fact]
+ public void SerializeExamplesDoesNotThrowNullReferenceException()
+ {
+ OpenApiDocument doc = new OpenApiDocument
+ {
+ Paths = new OpenApiPaths
+ {
+ ["test"] = new OpenApiPathItem()
+ {
+ Operations = new Dictionary()
+ {
+ [OperationType.Post] = new OpenApiOperation
+ {
+ RequestBody = new OpenApiRequestBody()
+ {
+ Content =
+ {
+ ["application/json"] = new OpenApiMediaType()
+ {
+ Examples = null,
+ },
+ }
+ }
+ },
+ }
+ },
+ }
+ };
+
+ OpenApiJsonWriter apiWriter = new OpenApiJsonWriter(new StringWriter());
+ doc.Invoking(d => d.SerializeAsV3(apiWriter)).Should().NotThrow();
+ }
+ }
}