diff --git a/src/Microsoft.OpenApi.Readers/OpenApiReaderSettings.cs b/src/Microsoft.OpenApi.Readers/OpenApiReaderSettings.cs
index 8821aa8c3..092699857 100644
--- a/src/Microsoft.OpenApi.Readers/OpenApiReaderSettings.cs
+++ b/src/Microsoft.OpenApi.Readers/OpenApiReaderSettings.cs
@@ -1,4 +1,7 @@
-using Microsoft.OpenApi.Any;
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// Licensed under the MIT license.
+
+using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Readers.ParseNodes;
using Microsoft.OpenApi.Validations;
diff --git a/src/Microsoft.OpenApi.Workbench/MainModel.cs b/src/Microsoft.OpenApi.Workbench/MainModel.cs
index 7906d88d7..6304b7f23 100644
--- a/src/Microsoft.OpenApi.Workbench/MainModel.cs
+++ b/src/Microsoft.OpenApi.Workbench/MainModel.cs
@@ -4,6 +4,7 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
+using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.OpenApi.Extensions;
@@ -11,6 +12,7 @@
using Microsoft.OpenApi.Readers;
using Microsoft.OpenApi.Services;
using Microsoft.OpenApi.Validations;
+using Microsoft.OpenApi.Writers;
namespace Microsoft.OpenApi.Workbench
{
@@ -31,6 +33,11 @@ public class MainModel : INotifyPropertyChanged
private string _renderTime;
+ ///
+ /// Default format.
+ ///
+ private bool _Inline = false;
+
///
/// Default format.
///
@@ -112,6 +119,16 @@ public OpenApiFormat Format
}
}
+ public bool Inline
+ {
+ get => _Inline;
+ set
+ {
+ _Inline = value;
+ OnPropertyChanged(nameof(Inline));
+ }
+ }
+
public OpenApiSpecVersion Version
{
get => _version;
@@ -232,11 +249,15 @@ internal void ParseDocument()
private string WriteContents(OpenApiDocument document)
{
var outputStream = new MemoryStream();
+
document.Serialize(
outputStream,
Version,
- Format);
-
+ Format,
+ new OpenApiWriterSettings() {
+ ReferenceInline = this.Inline == true ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
+ });
+
outputStream.Position = 0;
return new StreamReader(outputStream).ReadToEnd();
diff --git a/src/Microsoft.OpenApi.Workbench/MainWindow.xaml b/src/Microsoft.OpenApi.Workbench/MainWindow.xaml
index 21aba30e4..daf8a2209 100644
--- a/src/Microsoft.OpenApi.Workbench/MainWindow.xaml
+++ b/src/Microsoft.OpenApi.Workbench/MainWindow.xaml
@@ -42,6 +42,7 @@
+
diff --git a/src/Microsoft.OpenApi/Extensions/OpenAPIWriterExtensions.cs b/src/Microsoft.OpenApi/Extensions/OpenAPIWriterExtensions.cs
new file mode 100644
index 000000000..a32807ab6
--- /dev/null
+++ b/src/Microsoft.OpenApi/Extensions/OpenAPIWriterExtensions.cs
@@ -0,0 +1,26 @@
+using Microsoft.OpenApi.Writers;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Microsoft.OpenApi
+{
+ internal static class OpenAPIWriterExtensions
+ {
+ ///
+ /// Temporary extension method until we add Settings property to IOpenApiWriter in next major version
+ ///
+ ///
+ ///
+ internal static OpenApiWriterSettings GetSettings(this IOpenApiWriter openApiWriter)
+ {
+ if (openApiWriter is OpenApiWriterBase)
+ {
+ return ((OpenApiWriterBase)openApiWriter).Settings;
+ }
+ return new OpenApiWriterSettings();
+ }
+ }
+}
diff --git a/src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs b/src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs
index 5fc49a53d..f50f1c5f7 100644
--- a/src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs
+++ b/src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs
@@ -54,7 +54,8 @@ public static void Serialize(
this T element,
Stream stream,
OpenApiSpecVersion specVersion,
- OpenApiFormat format)
+ OpenApiFormat format,
+ OpenApiWriterSettings settings = null)
where T : IOpenApiSerializable
{
if (stream == null)
@@ -67,10 +68,10 @@ public static void Serialize(
switch (format)
{
case OpenApiFormat.Json:
- writer = new OpenApiJsonWriter(streamWriter);
+ writer = new OpenApiJsonWriter(streamWriter,settings);
break;
case OpenApiFormat.Yaml:
- writer = new OpenApiYamlWriter(streamWriter);
+ writer = new OpenApiYamlWriter(streamWriter, settings);
break;
default:
throw new OpenApiException(string.Format(SRResource.OpenApiFormatNotSupported, format));
@@ -86,6 +87,7 @@ public static void Serialize(
/// The Open API element.
/// The output writer.
/// Version of the specification the output should conform to
+
public static void Serialize(this T element, IOpenApiWriter writer, OpenApiSpecVersion specVersion)
where T : IOpenApiSerializable
{
@@ -116,6 +118,7 @@ public static void Serialize(this T element, IOpenApiWriter writer, OpenApiSp
writer.Flush();
}
+
///
/// Serializes the to the Open API document as a string in JSON format.
///
diff --git a/src/Microsoft.OpenApi/Models/OpenApiCallback.cs b/src/Microsoft.OpenApi/Models/OpenApiCallback.cs
index 512202691..4f685d2de 100644
--- a/src/Microsoft.OpenApi/Models/OpenApiCallback.cs
+++ b/src/Microsoft.OpenApi/Models/OpenApiCallback.cs
@@ -70,7 +70,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
throw Error.ArgumentNull(nameof(writer));
}
- if (Reference != null)
+ if (Reference != null && writer.GetSettings().ReferenceInline != ReferenceInlineSetting.InlineLocalReferences)
{
Reference.SerializeAsV3(writer);
return;
diff --git a/src/Microsoft.OpenApi/Models/OpenApiComponents.cs b/src/Microsoft.OpenApi/Models/OpenApiComponents.cs
index 6c114f722..08b8bd020 100644
--- a/src/Microsoft.OpenApi/Models/OpenApiComponents.cs
+++ b/src/Microsoft.OpenApi/Models/OpenApiComponents.cs
@@ -1,7 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
+using System;
using System.Collections.Generic;
+using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
@@ -76,6 +78,28 @@ public void SerializeAsV3(IOpenApiWriter writer)
throw Error.ArgumentNull(nameof(writer));
}
+ // If references have been inlined we don't need the to render the components section
+ // however if they have cycles, then we will need a component rendered
+ if (writer.GetSettings().ReferenceInline != ReferenceInlineSetting.DoNotInlineReferences)
+ {
+ var loops = writer.GetSettings().LoopDetector.Loops;
+ writer.WriteStartObject();
+ if (loops.TryGetValue(typeof(OpenApiSchema), out List