From 81ba78cc706ba0ef53fe2435d97bef6cc9c0ca4b Mon Sep 17 00:00:00 2001 From: Garrett Serack Date: Mon, 6 Jun 2016 08:26:24 -0700 Subject: [PATCH] Fixes #881 - failure to accept x-* vendor extension in path (#1122) * Fixes #881 - failure to accept x-* vendor extension in path * fixed code analysis issue * code analysis again --- .../AutoRest.Modeler.Swagger.Tests.csproj | 6 ++- .../Swagger/vendor-extension-in-path.json | 47 +++++++++++++++++++ .../Swagger.Tests/VendorExtensionInPath.cs | 30 ++++++++++++ .../JsonConverters/PathItemRefConverter.cs | 7 +++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 AutoRest/Modelers/Swagger.Tests/Swagger/vendor-extension-in-path.json create mode 100644 AutoRest/Modelers/Swagger.Tests/VendorExtensionInPath.cs diff --git a/AutoRest/Modelers/Swagger.Tests/AutoRest.Modeler.Swagger.Tests.csproj b/AutoRest/Modelers/Swagger.Tests/AutoRest.Modeler.Swagger.Tests.csproj index 2db5e127e7a87..db9c2bf5ac02e 100644 --- a/AutoRest/Modelers/Swagger.Tests/AutoRest.Modeler.Swagger.Tests.csproj +++ b/AutoRest/Modelers/Swagger.Tests/AutoRest.Modeler.Swagger.Tests.csproj @@ -36,6 +36,7 @@ True Resources.resx + @@ -90,6 +91,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest @@ -155,7 +159,7 @@ - + diff --git a/AutoRest/Modelers/Swagger.Tests/Swagger/vendor-extension-in-path.json b/AutoRest/Modelers/Swagger.Tests/Swagger/vendor-extension-in-path.json new file mode 100644 index 0000000000000..94e8a57479fc1 --- /dev/null +++ b/AutoRest/Modelers/Swagger.Tests/Swagger/vendor-extension-in-path.json @@ -0,0 +1,47 @@ +{ + "swagger": "2.0", + "info": { + "version": "2.0", + "title": "My web service", + "description": "No description provided.", + "x-endpoint-name": "default" + }, + "host": "dsw12.net:999", + "basePath": "/sandbox/8c88389038102937482abf83f8f/services/5b737fefc19b3047ab4f3234cd34642df2", + "schemes": [ + "https" + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "paths": { + "/swagger.json": { + "get": { + "summary": "Get swagger API document for the web service", + "operationId": "getSwaggerDocument", + "parameters": [ + { + "name": "api-version", + "in": "query", + "description": "API version", + "required": false, + "type": "string", + "default": "2.0", + "enum": [ + "2.0" + ] + } + ], + "responses": { + "200": { + "description": "Swagger API document for this service" + } + } + } + }, + "x-vendor-testy": "asd" + } +} \ No newline at end of file diff --git a/AutoRest/Modelers/Swagger.Tests/VendorExtensionInPath.cs b/AutoRest/Modelers/Swagger.Tests/VendorExtensionInPath.cs new file mode 100644 index 0000000000000..22f880708fe74 --- /dev/null +++ b/AutoRest/Modelers/Swagger.Tests/VendorExtensionInPath.cs @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using System.IO; +using Microsoft.Rest.Generator; +using Xunit; + +namespace Microsoft.Rest.Modeler.Swagger.Tests +{ + [Collection("AutoRest Tests")] + public class VendorExtensionInPath + { + [Fact] + public void AllowVendorExtensionInPath() + { + SwaggerModeler modeler = new SwaggerModeler(new Settings + { + Namespace = "Test", + Input = Path.Combine("Swagger", "vendor-extension-in-path.json") + }); + var clientModel = modeler.Build(); + + // should return a valid model. + Assert.NotNull(clientModel); + + // there should be one method in this generated api. + Assert.Equal(1, modeler.ServiceClient.Methods.Count); + } + } +} \ No newline at end of file diff --git a/AutoRest/Modelers/Swagger/JsonConverters/PathItemRefConverter.cs b/AutoRest/Modelers/Swagger/JsonConverters/PathItemRefConverter.cs index 8c105f58e901a..483c85d3076ea 100644 --- a/AutoRest/Modelers/Swagger/JsonConverters/PathItemRefConverter.cs +++ b/AutoRest/Modelers/Swagger/JsonConverters/PathItemRefConverter.cs @@ -25,6 +25,13 @@ public override bool CanConvert(System.Type objectType) public override object ReadJson(JsonReader reader, System.Type objectType, object existingValue, JsonSerializer serializer) { + // is the leaf an vendor extension? "x-*..." + if (reader == null || reader.Path.Substring(reader.Path.LastIndexOf(".", StringComparison.Ordinal) + 1).StartsWith("x-",StringComparison.CurrentCulture)) + { + // skip x-* vendor extensions when used where the path would be. + return new Dictionary < string, Operation >(); + } + JObject jobject = JObject.Load(reader); if (jobject == null) {