diff --git a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/AcceptanceTests.cs b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/AcceptanceTests.cs index 44f69af9cdadd..a7b6232b58294 100644 --- a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/AcceptanceTests.cs +++ b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/AcceptanceTests.cs @@ -34,5 +34,12 @@ public static void Batch() SwaggerSpecHelper.RunTests( SwaggerFile("BatchManagement.json"), ExpectedFolder("Batch")); } + + [Fact] + public static void Cdn() + { + SwaggerSpecHelper.RunTests( + SwaggerFile("cdn.json"), ExpectedFolder("CDN")); + } } } diff --git a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/AutoRest.Generator.AzureResourceSchema.Tests.csproj b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/AutoRest.Generator.AzureResourceSchema.Tests.csproj index 062740c250e38..8f0d851228ece 100644 --- a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/AutoRest.Generator.AzureResourceSchema.Tests.csproj +++ b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/AutoRest.Generator.AzureResourceSchema.Tests.csproj @@ -67,6 +67,7 @@ + @@ -82,7 +83,9 @@ PreserveNewest - + + PreserveNewest + PreserveNewest @@ -92,7 +95,9 @@ PreserveNewest - + + PreserveNewest + diff --git a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/Expected/Batch/Microsoft.Batch.json b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/Expected/Batch/Microsoft.Batch.json index 3c484e0bddc3f..286e7c21c44f4 100644 --- a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/Expected/Batch/Microsoft.Batch.json +++ b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/Expected/Batch/Microsoft.Batch.json @@ -8,11 +8,13 @@ "type": "object", "properties": { "type": { + "type": "string", "enum": [ "Microsoft.Batch/batchAccounts" ] }, "apiVersion": { + "type": "string", "enum": [ "2015-12-01" ] diff --git a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/Expected/CDN/Microsoft.Cdn.json b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/Expected/CDN/Microsoft.Cdn.json index 843259e861ab1..8af4ea9ade278 100644 --- a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/Expected/CDN/Microsoft.Cdn.json +++ b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/Expected/CDN/Microsoft.Cdn.json @@ -4,52 +4,54 @@ "title": "Microsoft.Cdn", "description": "Microsoft Cdn Resource Types", "resourceDefinitions": { - "profiles": { + "customDomains": { "type": "object", "properties": { "type": { + "type": "string", "enum": [ - "Microsoft.Cdn/profiles" + "Microsoft.Cdn/profiles/endpoints/customDomains" ] }, "apiVersion": { + "type": "string", "enum": [ "2016-04-02" ] }, - "location": { - "type": "string", - "description": "Profile location" - }, - "tags": { + "properties": { "type": "object", - "additionalProperties": { - "type": "string" + "properties": { + "hostName": { + "type": "string", + "description": "The host name of the custom domain. Must be a domain name." + } }, - "description": "Profile tags" - }, - "sku": { - "$ref": "#/definitions/Sku", - "description": "Profile SKU" + "required": [ + "hostName" + ] } }, "required": [ - "location", - "sku" + "type", + "apiVersion", + "properties" ], - "description": "Profile properties required for profile creation." + "description": "Microsoft.Cdn/profiles/endpoints/customDomains" }, "endpoints": { "type": "object", "properties": { "type": { + "type": "string", "enum": [ "Microsoft.Cdn/profiles/endpoints" ] }, "apiVersion": { + "type": "string", "enum": [ - "2015-06-01" + "2016-04-02" ] }, "location": { @@ -64,63 +66,145 @@ "description": "Endpoint tags" }, "properties": { - "originHostHeader": { - "type": "string", - "description": "The host header CDN provider will send along with content requests to origins. The default value is the host name of the origin." - }, - "originPath": { - "type": "string", - "description": "The path used for origin requests." - }, - "contentTypesToCompress": { - "type": "array", - "items": { - "type": "string" + "type": "object", + "properties": { + "originHostHeader": { + "type": "string", + "description": "The host header CDN provider will send along with content requests to origins. The default value is the host name of the origin." }, - "description": "List of content types on which compression will be applied. The value for the elements should be a valid MIME type." - }, - "isCompressionEnabled": { - "type": "boolean", - "description": "Indicates whether content compression is enabled. Default value is false. If compression is enabled, the content transferred from the CDN endpoint to the end user will be compressed. The requested content must be larger than 1 byte and smaller than 1 MB." - }, - "isHttpAllowed": { - "type": "boolean", - "description": "Indicates whether HTTP traffic is allowed on the endpoint. Default value is true. At least one protocol (HTTP or HTTPS) must be allowed." - }, - "isHttpsAllowed": { - "type": "boolean", - "description": "Indicates whether https traffic is allowed on the endpoint. Default value is true. At least one protocol (HTTP or HTTPS) must be allowed." - }, - "queryStringCachingBehavior": { - "$ref": "#/definitions/QueryStringCachingBehavior", - "description": "Defines the query string caching behavior." - }, - "origins": { - "type": "array", - "items": { - "$ref": "#/definitions/DeepCreatedOrigin" + "originPath": { + "type": "string", + "description": "The path used for origin requests." + }, + "contentTypesToCompress": { + "type": "array", + "items": { + "type": "string" + }, + "description": "List of content types on which compression will be applied. The value for the elements should be a valid MIME type." + }, + "isCompressionEnabled": { + "type": "boolean", + "description": "Indicates whether content compression is enabled. Default value is false. If compression is enabled, the content transferred from the CDN endpoint to the end user will be compressed. The requested content must be larger than 1 byte and smaller than 1 MB." + }, + "isHttpAllowed": { + "type": "boolean", + "description": "Indicates whether HTTP traffic is allowed on the endpoint. Default value is true. At least one protocol (HTTP or HTTPS) must be allowed." + }, + "isHttpsAllowed": { + "type": "boolean", + "description": "Indicates whether https traffic is allowed on the endpoint. Default value is true. At least one protocol (HTTP or HTTPS) must be allowed." + }, + "queryStringCachingBehavior": { + "type": "string", + "enum": [ + "IgnoreQueryString", + "BypassCaching", + "UseQueryString", + "NotSet" + ], + "description": "Defines the query string caching behavior. Possible values include: 'IgnoreQueryString', 'BypassCaching', 'UseQueryString', 'NotSet'" + }, + "origins": { + "type": "array", + "items": { + "$ref": "#/definitions/DeepCreatedOrigin" + } } - } - }, - "required": [ - "origins" - ] + }, + "required": [ + "origins" + ] + } }, "required": [ + "type", + "apiVersion", + "properties", "location" ], - "description": "Endpoint properties required for new endpoint creation." + "description": "Microsoft.Cdn/profiles/endpoints" }, "origins": { "type": "object", "properties": { - + "type": { + "type": "string", + "enum": [ + "Microsoft.Cdn/profiles/endpoints/origins" + ] + }, + "apiVersion": { + "type": "string", + "enum": [ + "2016-04-02" + ] + }, + "properties": { + "type": "object", + "properties": { + "hostName": { + "type": "string", + "description": "The address of the origin. Domain names, IPv4 addresses, and IPv6 addresses are supported." + }, + "httpPort": { + "type": "integer", + "description": "The value of the HTTP port. Must be between 1 and 65535." + }, + "httpsPort": { + "type": "integer", + "description": "The value of the HTTPS port. Must be between 1 and 65535." + } + }, + "required": [ + "hostName" + ] + } }, "required": [ + "type", + "apiVersion", + "properties" ], - "description": "Origin properties needed for origin creation or update." + "description": "Microsoft.Cdn/profiles.endpoints/origins" }, - "customDomains": { + "profiles": { + "type": "object", + "properties": { + "type": { + "enum": [ + "Microsoft.Cdn/profiles" + ] + }, + "apiVersion": { + "enum": [ + "2016-04-02" + ] + }, + "location": { + "type": "string", + "description": "Profile location" + }, + "tags": { + "type": "object", + "additionalProperties": { + "type": "string" + }, + "description": "Profile tags" + }, + "sku": { + "$ref": "#/definitions/Sku", + "description": "The SKU (pricing tier) of the CDN profile." + } + }, + "required": [ + "type", + "apiVersion", + "properties", + "location", + "sku" + ], + "description": "Microsoft.Cdn/profiles" } }, "definitions": { diff --git a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/Expected/Storage/Microsoft.Storage.json b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/Expected/Storage/Microsoft.Storage.json index b40222f7a88dc..fd5d7955160a6 100644 --- a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/Expected/Storage/Microsoft.Storage.json +++ b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/Expected/Storage/Microsoft.Storage.json @@ -8,11 +8,13 @@ "type": "object", "properties": { "type": { + "type": "string", "enum": [ "Microsoft.Storage/storageAccounts" ] }, "apiVersion": { + "type": "string", "enum": [ "2016-01-01" ] diff --git a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/JSONSchemaTests.cs b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/JSONSchemaTests.cs new file mode 100644 index 0000000000000..fe791fe293630 --- /dev/null +++ b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema.Tests/JSONSchemaTests.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using Microsoft.Rest.Generator.AzureResourceSchema; +using Xunit; + +namespace AutoRest.Generator.AzureResourceSchema.Tests +{ + public class JSONSchemaTests + { + [Fact] + public void AddProperty() + { + JSONSchema schema = new JSONSchema(); + JSONSchema age = new JSONSchema() + { + Type = "number" + }; + schema.AddProperty("age", age); + Assert.Same(age, schema.Properties["age"]); + } + } +} diff --git a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/AutoRest.Generator.AzureResourceSchema.csproj b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/AutoRest.Generator.AzureResourceSchema.csproj index efd0dc9af685d..6c81c828ff292 100644 --- a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/AutoRest.Generator.AzureResourceSchema.csproj +++ b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/AutoRest.Generator.AzureResourceSchema.csproj @@ -30,6 +30,7 @@ + diff --git a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/AzureResourceSchemaCodeGenerator.cs b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/AzureResourceSchemaCodeGenerator.cs index cc97d603ec23a..f4baa7d2dc69d 100644 --- a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/AzureResourceSchemaCodeGenerator.cs +++ b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/AzureResourceSchemaCodeGenerator.cs @@ -54,6 +54,8 @@ public override void NormalizeClientModel(ServiceClient serviceClient) public override async Task Generate(ServiceClient serviceClient) { + ResourceSchema schema = ResourceSchema.Parse(serviceClient); + StringWriter stringWriter = new StringWriter(); using (JsonTextWriter writer = new JsonTextWriter(stringWriter)) { @@ -62,23 +64,21 @@ public override async Task Generate(ServiceClient serviceClient) writer.IndentChar = ' '; writer.QuoteChar = '\"'; - ResourceSchema schema = ResourceSchema.Parse(serviceClient); - - WriteSchema(writer, schema); + WriteResourceSchema(writer, schema); } await Write(stringWriter.ToString(), SchemaPath); } - private static void WriteSchema(JsonTextWriter writer, ResourceSchema schema) + private static void WriteResourceSchema(JsonTextWriter writer, ResourceSchema schema) { WriteObject(writer, () => { - WriteStringProperty(writer, "id", schema.Id); - WriteStringProperty(writer, "$schema", "http://json-schema.org/draft-04/schema#"); - WriteStringProperty(writer, "title", schema.Title); - WriteStringProperty(writer, "description", schema.Description); - WriteObjectProperty(writer, "resourceDefinitions", () => + WriteJsonStringProperty(writer, "id", schema.Id); + WriteJsonStringProperty(writer, "$schema", schema.Schema); + WriteJsonStringProperty(writer, "title", schema.Title); + WriteJsonStringProperty(writer, "description", schema.Description); + WriteJsonObjectProperty(writer, "resourceDefinitions", () => { foreach (Resource resource in schema.Resources) { @@ -88,33 +88,90 @@ private static void WriteSchema(JsonTextWriter writer, ResourceSchema schema) if (schema.Definitions.Count() > 0) { - WriteObjectProperty(writer, "definitions", () => + WriteJsonObjectProperty(writer, "definitions", () => { foreach (Definition definition in schema.Definitions.Values.OrderBy(definition => definition.Name)) { - WriteObjectProperty(writer, definition.Name, null, definition, schema.Definitions.Keys); + WriteJsonObjectProperty(writer, definition.Name, null, definition, schema.Definitions.Keys); + } + }); + } + }); + } + + private static void WriteJsonStringProperty(JsonTextWriter writer, string propertyName, string propertyValue) + { + writer.WritePropertyName(propertyName); + writer.WriteValue(propertyValue); + } + + private static void WriteJsonObjectProperty(JsonTextWriter writer, string propertyName, string propertyDescription, Definition propertyDefinition, IEnumerable schemaDefinitionNames) + { + WriteJsonObjectProperty(writer, propertyName, () => + { + Debug.Assert(!String.IsNullOrWhiteSpace(propertyDefinition.DefinitionType)); + WriteJsonStringProperty(writer, "type", propertyDefinition.DefinitionType); + + if (propertyDefinition.ArrayElement != null) + { + WriteJsonObjectProperty(writer, "items", null, propertyDefinition.ArrayElement, schemaDefinitionNames); + } + + if (propertyDefinition.AllowedValues != null) + { + WriteArrayProperty(writer, "enum", propertyDefinition.AllowedValues); + } + + if (propertyDefinition.AdditionalProperties != null) + { + WriteJsonObjectProperty(writer, "additionalProperties", null, propertyDefinition.AdditionalProperties, schemaDefinitionNames); + } + + if (propertyDefinition.Properties != null) + { + WriteJsonObjectProperty(writer, "properties", () => + { + foreach (SchemaProperty definitionProperty in propertyDefinition.Properties) + { + WriteResourceProperty(writer, definitionProperty, schemaDefinitionNames); } }); + + if (propertyDefinition.RequiredPropertyNames != null) + { + WriteArrayProperty(writer, "required", propertyDefinition.RequiredPropertyNames); + } + } + + if (propertyDescription != null) + { + WriteJsonStringProperty(writer, "description", propertyDescription); + } + else if (propertyDefinition.Description != null) + { + WriteJsonStringProperty(writer, "description", propertyDefinition.Description); } }); } private static void WriteResource(JsonTextWriter writer, Resource resource, IEnumerable schemaDefinitionNames) { - WriteObjectProperty(writer, resource.Name, () => + WriteJsonObjectProperty(writer, resource.Name, () => { - WriteStringProperty(writer, "type", "object"); + WriteJsonStringProperty(writer, "type", "object"); // Root level properties of the resource - WriteObjectProperty(writer, "properties", () => + WriteJsonObjectProperty(writer, "properties", () => { - WriteObjectProperty(writer, "type", () => + WriteJsonObjectProperty(writer, "type", () => { + WriteJsonStringProperty(writer, "type", "string"); WriteArrayProperty(writer, "enum", new string[] { resource.ResourceType }); }); - WriteObjectProperty(writer, "apiVersion", () => + WriteJsonObjectProperty(writer, "apiVersion", () => { + WriteJsonStringProperty(writer, "type", "string"); WriteArrayProperty(writer, "enum", resource.ApiVersions); }); @@ -127,39 +184,42 @@ private static void WriteResource(JsonTextWriter writer, Resource resource, IEnu } }); WriteArrayProperty(writer, "required", resource.RequiredPropertyNames); - WriteStringProperty(writer, "description", resource.Description); + WriteJsonStringProperty(writer, "description", resource.Description); }); } private static void WriteResourceProperty(JsonTextWriter writer, SchemaProperty property, IEnumerable schemaDefinitionNames) { Definition definition = property.Definition; - if (!property.ShouldFlatten && schemaDefinitionNames.Contains(definition.Name)) + //if (!property.ShouldFlatten) { - WriteObjectProperty(writer, property.Name, () => + if (schemaDefinitionNames.Contains(definition.Name)) { - WriteStringProperty(writer, "$ref", "#/definitions/" + definition.Name); - if (property.Description != null) + WriteJsonObjectProperty(writer, property.Name, () => { - WriteStringProperty(writer, "description", property.Description); - } - }); - } - else if(!property.ShouldFlatten) - { - WriteObjectProperty(writer, property.Name, property.Description, definition, schemaDefinitionNames); - } - else - { - Debug.Assert(property.Name == "properties"); - WriteObjectProperty(writer, "properties", () => + WriteJsonStringProperty(writer, "$ref", "#/definitions/" + definition.Name); + if (property.Description != null) + { + WriteJsonStringProperty(writer, "description", property.Description); + } + }); + } + else { - foreach (SchemaProperty definitionProperty in definition.Properties) - { - WriteResourceProperty(writer, definitionProperty, schemaDefinitionNames); - } - }); + WriteJsonObjectProperty(writer, property.Name, property.Description, definition, schemaDefinitionNames); + } } + //else + //{ + // Debug.Assert(property.Name == "properties"); + // WriteObjectProperty(writer, "properties", () => + // { + // foreach (SchemaProperty definitionProperty in definition.Properties) + // { + // WriteResourceProperty(writer, definitionProperty, schemaDefinitionNames); + // } + // }); + //} } //private static void WriteObjectOrExpression(JsonTextWriter writer, Action writeObjectContents) @@ -193,62 +253,7 @@ private static void WriteArrayProperty(JsonTextWriter writer, string propertyNam writer.WriteEndArray(); } - private static void WriteStringProperty(JsonTextWriter writer, string propertyName, string propertyValue) - { - writer.WritePropertyName(propertyName); - writer.WriteValue(propertyValue); - } - - private static void WriteObjectProperty(JsonTextWriter writer, string propertyName, string propertyDescription, Definition propertyDefinition, IEnumerable schemaDefinitionNames) - { - WriteObjectProperty(writer, propertyName, () => - { - Debug.Assert(!String.IsNullOrWhiteSpace(propertyDefinition.DefinitionType)); - WriteStringProperty(writer, "type", propertyDefinition.DefinitionType); - - if (propertyDefinition.ArrayElement != null) - { - WriteObjectProperty(writer, "items", null, propertyDefinition.ArrayElement, schemaDefinitionNames); - } - - if (propertyDefinition.AllowedValues != null) - { - WriteArrayProperty(writer, "enum", propertyDefinition.AllowedValues); - } - - if (propertyDefinition.AdditionalProperties != null) - { - WriteObjectProperty(writer, "additionalProperties", null, propertyDefinition.AdditionalProperties, schemaDefinitionNames); - } - - if (propertyDefinition.Properties != null) - { - WriteObjectProperty(writer, "properties", () => - { - foreach (SchemaProperty definitionProperty in propertyDefinition.Properties) - { - WriteResourceProperty(writer, definitionProperty, schemaDefinitionNames); - } - }); - - if (propertyDefinition.RequiredPropertyNames != null) - { - WriteArrayProperty(writer, "required", propertyDefinition.RequiredPropertyNames); - } - } - - if (propertyDescription != null) - { - WriteStringProperty(writer, "description", propertyDescription); - } - else if (propertyDefinition.Description != null) - { - WriteStringProperty(writer, "description", propertyDefinition.Description); - } - }); - } - - private static void WriteObjectProperty(JsonTextWriter writer, string propertyName, Action writeObjectContents) + private static void WriteJsonObjectProperty(JsonTextWriter writer, string propertyName, Action writeObjectContents) { writer.WritePropertyName(propertyName); WriteObject(writer, writeObjectContents); diff --git a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/JSONSchema.cs b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/JSONSchema.cs new file mode 100644 index 0000000000000..0858f9df5801c --- /dev/null +++ b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/JSONSchema.cs @@ -0,0 +1,63 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using System.Collections.Generic; +using System.Diagnostics; + +namespace Microsoft.Rest.Generator.AzureResourceSchema +{ + /// + /// An object representing a JSON schema. Each property of a JSON schema ($schema, title, and + /// description are metadata, not properties) is also a JSON schema, so the class is recursive. + /// + public class JSONSchema + { + /// + /// The $schema metadata that points to a URL or file location where this schema's schema is stored. + /// + public string Schema { get; set; } + + /// + /// The title metadata for this schema. + /// + public string Title { get; set; } + + /// + /// The description metadata that describes this schema. + /// + public string Description { get; set; } + + /// + /// The type metadata of this schema that describes what type matching JSON values must be. + /// + public string Type { get; set; } + + /// + /// The schemas that describe the properties of a matching JSON value. + /// + public IDictionary Properties { get; set; } + + /// + /// The names of the properties that are required for a matching JSON value. + /// + public IEnumerable Required { get; set; } + + /// + /// Add the provided property to this JSON schema. + /// + /// The name of the property to add. + /// The schema of the property to add. + public void AddProperty(string propertyName, JSONSchema propertySchema) + { + Debug.Assert(!string.IsNullOrWhiteSpace(propertyName)); + Debug.Assert(Properties == null || !Properties.ContainsKey(propertyName)); + Debug.Assert(propertySchema != null); + + if (Properties == null) + { + Properties = new Dictionary(); + } + Properties.Add(propertyName, propertySchema); + } + } +} diff --git a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/ResourceSchema.cs b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/ResourceSchema.cs index f2aea28f321fc..c98a95bbb83e6 100644 --- a/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/ResourceSchema.cs +++ b/AutoRest/Generators/AzureResourceSchema/AzureResourceSchema/ResourceSchema.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; -using System.Linq; namespace Microsoft.Rest.Generator.AzureResourceSchema { @@ -16,6 +15,8 @@ public class ResourceSchema public string Id { get; set; } + public readonly string Schema = "http://json-schema.org/draft-04/schema#"; + public string Title { get; set; } public string Description { get; set; } @@ -78,27 +79,43 @@ public static ResourceSchema Parse(ServiceClient serviceClient) Debug.Assert(resourceProvider == null || resourceProvider == resourceMethodProvider); resourceProvider = resourceMethodProvider; + resource.ResourceType = resourceProvider; + // Get resource's name int resourceNameStartIndex = forwardSlashIndexAfterProvider + 1; - int forwardSlashIndexAfterResourceName = afterPrefix.IndexOf('/', resourceNameStartIndex); - if (forwardSlashIndexAfterResourceName == -1) - { - resource.Name = afterPrefix.Substring(resourceNameStartIndex); - } - else + string afterProvider = afterPrefix.Substring(resourceNameStartIndex); + int resourceTypeSegmentStartIndex = 0; + while(resourceTypeSegmentStartIndex != -1) { - resource.Name = afterPrefix.Substring(resourceNameStartIndex, forwardSlashIndexAfterResourceName - resourceNameStartIndex); - } + int forwardSlashAfterResourceType = afterProvider.IndexOf('/', resourceTypeSegmentStartIndex); + if (forwardSlashAfterResourceType == -1) + { + resourceTypeSegmentStartIndex = -1; + } + else + { + string resourceTypeSegment = afterProvider.Substring(resourceTypeSegmentStartIndex, forwardSlashAfterResourceType - resourceTypeSegmentStartIndex); - // Get the resource's full type / - if (forwardSlashIndexAfterResourceName == -1) - { - resource.ResourceType = afterPrefix; - } - else - { - resource.ResourceType = afterPrefix.Substring(0, forwardSlashIndexAfterResourceName); + Debug.Assert(afterProvider[forwardSlashAfterResourceType + 1] == '{'); + + int forwardSlashAfterPlaceholder = afterProvider.IndexOf('/', forwardSlashAfterResourceType + 1); + if (forwardSlashAfterPlaceholder == -1) + { + Debug.Assert(afterProvider[afterProvider.Length - 1] == '}'); + + resource.Name = resourceTypeSegment; + resourceTypeSegmentStartIndex = -1; + } + else + { + Debug.Assert(afterProvider[forwardSlashAfterPlaceholder - 1] == '}'); + resourceTypeSegmentStartIndex = forwardSlashAfterPlaceholder + 1; + } + + resource.ResourceType += "/" + resourceTypeSegment; + } } + resource.Description = resource.ResourceType; // Get the resource's properties diff --git a/AutoRest/Modelers/Swagger.Tests/SwaggerSpecHelper.cs b/AutoRest/Modelers/Swagger.Tests/SwaggerSpecHelper.cs index ed656f10d44bc..0012a6f4892dc 100644 --- a/AutoRest/Modelers/Swagger.Tests/SwaggerSpecHelper.cs +++ b/AutoRest/Modelers/Swagger.Tests/SwaggerSpecHelper.cs @@ -89,6 +89,6 @@ private static void EnsureFilesMatch(string expectedFileContent, string actualFi { Assert.Equal(expectedLines[i], actualLines[i]); } - } + } } } \ No newline at end of file