From 73effb59aa14b7311ad7b14eac3d15a61ce64774 Mon Sep 17 00:00:00 2001 From: Louis DeJardin Date: Tue, 26 Mar 2019 16:35:23 -0700 Subject: [PATCH] Allow swagger.extra to be a string literal (#121) * Allow swagger.extra to be a string literal This makes complex scenarios possible where the extra data needs to contain handlebars template markup which is incompatible with a yaml file section * Adding unit test for extra literal string --- .../Swagger/RequestGenerator.cs | 9 ++- .../Swagger/SwaggerWorkflowTests.cs | 64 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.Atlas.CommandLine/Swagger/RequestGenerator.cs b/src/Microsoft.Atlas.CommandLine/Swagger/RequestGenerator.cs index f56e75f..fcd7435 100644 --- a/src/Microsoft.Atlas.CommandLine/Swagger/RequestGenerator.cs +++ b/src/Microsoft.Atlas.CommandLine/Swagger/RequestGenerator.cs @@ -82,7 +82,14 @@ namespace Microsoft.Atlas.CommandLine.Swagger if (context.SwaggerReference.extra != null) { - writer.WriteLine(_yamlSerializers.YamlSerializer.Serialize(context.SwaggerReference.extra)); + if (context.SwaggerReference.extra is string extraString) + { + writer.WriteLine(extraString); + } + else + { + writer.WriteLine(_yamlSerializers.YamlSerializer.Serialize(context.SwaggerReference.extra)); + } } var bodyParameter = parameters.SingleOrDefault(parameter => parameter.@in == "body"); diff --git a/test/Microsoft.Atlas.CommandLine.Tests/Swagger/SwaggerWorkflowTests.cs b/test/Microsoft.Atlas.CommandLine.Tests/Swagger/SwaggerWorkflowTests.cs index f2471d5..30ef688 100644 --- a/test/Microsoft.Atlas.CommandLine.Tests/Swagger/SwaggerWorkflowTests.cs +++ b/test/Microsoft.Atlas.CommandLine.Tests/Swagger/SwaggerWorkflowTests.cs @@ -388,6 +388,70 @@ Responses: stubHttpClients.AssertRequest("GET", "https://example.com/path"); } + [TestMethod] + public async Task ExtraCanBeStringLiteral() + { + var stubHttpClients = Yaml(@" +Responses: + https://example.net/the-test/readme.md: + GET: + status: 200 + body: | + ``` yaml + info: + title: TheTest + swagger: + foo: + target: apis/tests + source: https://example.org/specs/ + inputs: + - testing/swagger.json + extra: | + headers: + x-sum: {{{ query ""sum([`2`, `5`])"" }}} + ``` + https://example.net/the-test/workflow.yaml: + GET: + status: 200 + body: + operations: + - request: apis/tests/TestingClient/One/Two.yaml + https://example.org/specs/testing/swagger.json: + GET: + status: 200 + body: + swagger: 2.0 + info: + title: Testing Client + version: 5.0-preview.2 + host: example.com + paths: + /path: + get: + tags: + - One + operationId: One_Two + https://example.com/path: + GET: + status: 200 + body: + - request has arrived +"); + + InitializeServices(stubHttpClients); + + var result = Services.App.Execute("deploy", "https://example.net/the-test"); + + System.Console.Error.WriteLine(Console.ErrorStringWriter.ToString()); + System.Console.Out.WriteLine(Console.OutStringWriter.ToString()); + + Assert.AreEqual(0, result); + + var request = stubHttpClients.AssertRequest("GET", "https://example.com/path"); + var xSumValue = request.Headers.GetValues("x-sum").Single(); + Assert.AreEqual("7", xSumValue); + } + public class ServiceContext : ServiceContextBase { public CommandLineApplicationServices App { get; set; }