2017-10-19 23:39:57 +02:00
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
|
// <copyright file="OpenApiWriterTestHelper.cs" company="Microsoft">
|
|
|
|
|
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
|
|
|
|
|
// </copyright>
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2017-11-14 23:41:19 +01:00
|
|
|
|
using Microsoft.OpenApi.Interfaces;
|
|
|
|
|
using Microsoft.OpenApi.Writers;
|
2017-10-19 23:39:57 +02:00
|
|
|
|
|
2017-11-11 01:14:41 +01:00
|
|
|
|
namespace Microsoft.OpenApi.OData.Tests
|
2017-10-19 23:39:57 +02:00
|
|
|
|
{
|
|
|
|
|
internal static class OpenApiWriterTestHelper
|
|
|
|
|
{
|
2017-11-14 23:41:19 +01:00
|
|
|
|
internal static string WriteToJson(this IOpenApiElement element,
|
|
|
|
|
Action<IOpenApiWriter, IOpenApiElement> before = null,
|
|
|
|
|
Action<IOpenApiWriter, IOpenApiElement> after = null)
|
2017-10-19 23:39:57 +02:00
|
|
|
|
{
|
|
|
|
|
Action<IOpenApiWriter> action = writer =>
|
|
|
|
|
{
|
|
|
|
|
before?.Invoke(writer, element);
|
2017-11-11 00:45:25 +01:00
|
|
|
|
// element?(writer);
|
2017-10-19 23:39:57 +02:00
|
|
|
|
after?.Invoke(writer, element);
|
|
|
|
|
writer?.Flush();
|
|
|
|
|
};
|
|
|
|
|
|
2017-11-11 00:45:25 +01:00
|
|
|
|
return Write(OpenApiFormat.Json, action);
|
2017-10-19 23:39:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 23:41:19 +01:00
|
|
|
|
internal static string WriteToYaml(this IOpenApiElement element,
|
|
|
|
|
Action<IOpenApiWriter, IOpenApiElement> before = null,
|
|
|
|
|
Action<IOpenApiWriter, IOpenApiElement> after = null)
|
2017-10-19 23:39:57 +02:00
|
|
|
|
{
|
|
|
|
|
Action<IOpenApiWriter> action = writer =>
|
|
|
|
|
{
|
|
|
|
|
before?.Invoke(writer, element);
|
2017-11-11 00:45:25 +01:00
|
|
|
|
//element?.Write(writer);
|
2017-10-19 23:39:57 +02:00
|
|
|
|
after?.Invoke(writer, element);
|
|
|
|
|
writer?.Flush();
|
|
|
|
|
};
|
|
|
|
|
|
2017-11-11 00:45:25 +01:00
|
|
|
|
return Write(OpenApiFormat.Yaml, action);
|
2017-10-19 23:39:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 23:41:19 +01:00
|
|
|
|
internal static string Write(this IOpenApiElement element,
|
2017-11-11 00:45:25 +01:00
|
|
|
|
OpenApiFormat target,
|
2017-11-14 23:41:19 +01:00
|
|
|
|
Action<IOpenApiWriter, IOpenApiElement> before = null,
|
|
|
|
|
Action<IOpenApiWriter, IOpenApiElement> after = null)
|
2017-10-19 23:39:57 +02:00
|
|
|
|
{
|
|
|
|
|
Action<IOpenApiWriter> action = writer =>
|
|
|
|
|
{
|
|
|
|
|
before?.Invoke(writer, element);
|
2017-11-11 00:45:25 +01:00
|
|
|
|
// element?.Write(writer);
|
2017-10-19 23:39:57 +02:00
|
|
|
|
after?.Invoke(writer, element);
|
|
|
|
|
writer?.Flush();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Write(target, action);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-11 00:45:25 +01:00
|
|
|
|
internal static string Write(this Action<IOpenApiWriter> action, OpenApiFormat target)
|
2017-10-19 23:39:57 +02:00
|
|
|
|
{
|
|
|
|
|
MemoryStream stream = new MemoryStream();
|
|
|
|
|
IOpenApiWriter writer;
|
2017-11-11 00:45:25 +01:00
|
|
|
|
if (target == OpenApiFormat.Yaml)
|
2017-10-19 23:39:57 +02:00
|
|
|
|
{
|
|
|
|
|
writer = new OpenApiYamlWriter(new StreamWriter(stream));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
writer = new OpenApiJsonWriter(new StreamWriter(stream));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
action(writer);
|
|
|
|
|
writer.Flush();
|
|
|
|
|
stream.Position = 0;
|
|
|
|
|
return new StreamReader(stream).ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-11-11 00:45:25 +01:00
|
|
|
|
internal static string Write(OpenApiFormat target, Action<IOpenApiWriter> action)
|
2017-10-19 23:39:57 +02:00
|
|
|
|
{
|
|
|
|
|
MemoryStream stream = new MemoryStream();
|
|
|
|
|
IOpenApiWriter writer;
|
2017-11-11 00:45:25 +01:00
|
|
|
|
if (target == OpenApiFormat.Yaml)
|
2017-10-19 23:39:57 +02:00
|
|
|
|
{
|
|
|
|
|
writer = new OpenApiYamlWriter(new StreamWriter(stream));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
writer = new OpenApiJsonWriter(new StreamWriter(stream));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
action(writer);
|
|
|
|
|
writer.Flush();
|
|
|
|
|
stream.Position = 0;
|
|
|
|
|
return new StreamReader(stream).ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|