-
Notifications
You must be signed in to change notification settings - Fork 737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add treat default type as error #1654
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
{ | ||
"swagger": "2.0", | ||
"info": { | ||
"title": "Microsoft Azure Redis Cache Management API", | ||
"description": "Some cool documentation.", | ||
"version": "2014-04-01-preview" | ||
}, | ||
"host": "management.azure.com", | ||
"schemes": [ | ||
"https" | ||
], | ||
"basePath": "/", | ||
"produces": [ "application/json" ], | ||
"consumes": [ "application/json" ], | ||
"paths": { | ||
"/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/Microsoft.Cache/Redis?api-version={apiVersion}": { | ||
"get": { | ||
"operationId": "list", | ||
"summary": "Product Types", | ||
"description": "The Products endpoint returns information about the Uber products offered at a given location. The response includes the display name and other details about each product, and lists the products in the proper display order.", | ||
"parameters": [ | ||
{ | ||
"$ref": "#/parameters/SubscriptionIdParamterer" | ||
}, | ||
{ | ||
"name": "resourceGroupName", | ||
"in": "path", | ||
"description": "Resource Group ID.", | ||
"required": true, | ||
"type": "string" | ||
}, | ||
{ | ||
"$ref": "ApiVersionParameter" | ||
} | ||
], | ||
"tags": [ | ||
"Redis" | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "A list of caches", | ||
"schema": { | ||
"$ref": "Product" | ||
} | ||
}, | ||
"410": { | ||
"description": "This product has been removed", | ||
"schema": { | ||
"$ref": "Error" | ||
} | ||
}, | ||
"default": { | ||
"description": "Unexpected error", | ||
"schema": { | ||
"$ref": "Error" | ||
} | ||
} | ||
} | ||
}, | ||
"post": { | ||
"operationId": "reset", | ||
"summary": "Resets products", | ||
"description": "Resets products.", | ||
"parameters": [ | ||
{ | ||
"name": "subscriptionId", | ||
"in": "path", | ||
"description": "Subscription ID.", | ||
"required": true, | ||
"type": "string" | ||
}, | ||
{ | ||
"name": "resourceGroupName", | ||
"in": "path", | ||
"description": "Resource Group ID.", | ||
"required": true, | ||
"type": "string" | ||
}, | ||
{ | ||
"name": "apiVersion", | ||
"in": "path", | ||
"description": "API ID.", | ||
"required": true, | ||
"type": "string" | ||
} | ||
], | ||
"tags": [ | ||
"Redis" | ||
], | ||
"responses": { | ||
"204": { | ||
"description": "A list of caches", | ||
"examples": { | ||
"application/json": { | ||
"id": 9, | ||
"category": { | ||
"name": "domestic" | ||
}, | ||
"name": "monster", | ||
"tags": [ | ||
{ | ||
"name": "for sale" | ||
} | ||
], | ||
"status": "alive" | ||
} | ||
} | ||
}, | ||
"403": { | ||
"description": "User is forbidden to reset the products.", | ||
"schema": { | ||
"$ref": "Error" | ||
} | ||
}, | ||
"default": { | ||
"description": "Unexpected error", | ||
"schema": { | ||
"$ref": "Error" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"definitions": { | ||
"Product": { | ||
"title": "The product title.", | ||
"description": "The product documentation.", | ||
"properties": { | ||
"product_id": { | ||
"type": "string", | ||
"title": "A product id.", | ||
"description": "Unique identifier representing a specific product for a given latitude & longitude. For example, uberX in San Francisco will have a different product_id than uberX in Los Angeles." | ||
}, | ||
"description": { | ||
"type": "string", | ||
"description": "Description of product." | ||
}, | ||
"display_name": { | ||
"type": "string", | ||
"description": "Display name of product." | ||
}, | ||
"capacity": { | ||
"type": "string", | ||
"description": "Capacity of product. For example, 4 people.", | ||
"default": "100" | ||
}, | ||
"image": { | ||
"type": "string", | ||
"description": "Image URL representing the product." | ||
} | ||
}, | ||
"example": { | ||
"name": "Puma", | ||
"id": 1 | ||
} | ||
}, | ||
"Error": { | ||
"properties": { | ||
"code": { | ||
"type": "integer", | ||
"format": "int32" | ||
}, | ||
"message": { | ||
"type": "string" | ||
}, | ||
"fields": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
}, | ||
"parameters": { | ||
"SubscriptionIdParamterer": { | ||
"name": "subscriptionId", | ||
"in": "path", | ||
"description": "Subscription ID.", | ||
"required": true, | ||
"type": "string" | ||
}, | ||
"ApiVersionParameter": { | ||
"name": "apiVersion", | ||
"in": "path", | ||
"description": "API ID.", | ||
"required": true, | ||
"type": "string" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,14 +8,13 @@ | |
using System.Linq; | ||
using System.Net; | ||
using System.Text; | ||
using AutoRest.Core.Model; | ||
using AutoRest.Core.Logging; | ||
using AutoRest.Core.Model; | ||
using AutoRest.Core.Utilities; | ||
using AutoRest.Swagger.Model; | ||
using AutoRest.Swagger.Properties; | ||
using ParameterLocation = AutoRest.Swagger.Model.ParameterLocation; | ||
|
||
using static AutoRest.Core.Utilities.DependencyInjection; | ||
using ParameterLocation = AutoRest.Swagger.Model.ParameterLocation; | ||
|
||
namespace AutoRest.Swagger | ||
{ | ||
|
@@ -51,14 +50,13 @@ public Method BuildMethod(HttpMethod httpMethod, string url, string methodName, | |
{ | ||
EnsureUniqueMethodName(methodName, methodGroup); | ||
|
||
var method = New<Method>(new | ||
{ | ||
var method = New<Method>(new { | ||
HttpMethod = httpMethod, | ||
Url = url, | ||
Name = methodName, | ||
SerializedName = _operation.OperationId | ||
}); | ||
|
||
method.RequestContentType = _effectiveConsumes.FirstOrDefault() ?? APP_JSON_MIME; | ||
string produce = _effectiveConsumes.FirstOrDefault(s => s.StartsWith(APP_JSON_MIME, StringComparison.OrdinalIgnoreCase)); | ||
if (!string.IsNullOrEmpty(produce)) | ||
|
@@ -96,16 +94,12 @@ public Method BuildMethod(HttpMethod httpMethod, string url, string methodName, | |
|
||
var headerTypeName = string.Format(CultureInfo.InvariantCulture, | ||
"{0}-{1}-Headers", methodGroup, methodName).Trim('-'); | ||
var headerType = New<CompositeType>(headerTypeName,new | ||
{ | ||
var headerType = New<CompositeType>(headerTypeName, new { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switch to ANSI-style Braces |
||
SerializedName = headerTypeName, | ||
Documentation = string.Format(CultureInfo.InvariantCulture, "Defines headers for {0} operation.", methodName) | ||
}); | ||
responseHeaders.ForEach(h => | ||
{ | ||
|
||
var property = New<Property>(new | ||
{ | ||
responseHeaders.ForEach(h => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switch to ANSI-style Braces |
||
var property = New<Property>(new { | ||
Name = h.Key, | ||
SerializedName = h.Key, | ||
ModelType = h.Value.GetBuilder(this._swaggerModeler).BuildServiceType(h.Key), | ||
|
@@ -142,8 +136,7 @@ public Method BuildMethod(HttpMethod httpMethod, string url, string methodName, | |
private static IEnumerable<SwaggerParameter> DeduplicateParameters(IEnumerable<SwaggerParameter> parameters) | ||
{ | ||
return parameters | ||
.Select(s => | ||
{ | ||
.Select(s => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switch to ANSI-style Braces |
||
// if parameter with the same name exists in Body and Path/Query then we need to give it a unique name | ||
if (s.In == ParameterLocation.Body) | ||
{ | ||
|
@@ -222,10 +215,37 @@ private List<Stack<IModelType>> BuildResponses(Method method, CompositeType head | |
} | ||
} | ||
} | ||
|
||
RemoveResponsesWithDefaultType(method, typesList); | ||
return typesList; | ||
} | ||
|
||
private void RemoveResponsesWithDefaultType(Method method, List<Stack<IModelType>> typesList) | ||
{ | ||
var errorModelType = method.DefaultResponse.Body as CompositeType; | ||
if (errorModelType == null || method.Responses == null) return; | ||
var keys = method.Responses.Keys; | ||
var removeResponses = new List<HttpStatusCode>(); | ||
foreach (var key in keys) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The response types for error conditions should be related (ie, default uses That way we can have very specific types for errors, and makes it very clear what's an acceptable error. Of course that means we have to do three other things:
|
||
{ | ||
//Names have been disambiguated by this point. | ||
if (method.Responses[key] == null || method.Responses[key].Body == null) | ||
{ | ||
continue; | ||
} | ||
if (method.Responses[key].Body.Name == errorModelType.Name) | ||
{ | ||
removeResponses.Add(key); | ||
} | ||
} | ||
|
||
foreach (var key in removeResponses) | ||
{ | ||
method.Responses.Remove(key); | ||
} | ||
|
||
typesList.RemoveAll(foo => foo.Peek().Name.Equals(errorModelType.Name)); | ||
} | ||
|
||
private Response BuildMethodReturnType(List<Stack<IModelType>> types, IModelType headerType) | ||
{ | ||
IModelType baseType = New<PrimaryType>(KnownPrimaryType.Object); | ||
|
@@ -241,8 +261,7 @@ private Response BuildMethodReturnType(List<Stack<IModelType>> types, IModelType | |
} | ||
|
||
// BuildParameter up type inheritance tree | ||
types.ForEach(typeStack => | ||
{ | ||
types.ForEach(typeStack => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ANSI Braces Please. |
||
IModelType type = typeStack.Peek(); | ||
while (!Equals(type, baseType)) | ||
{ | ||
|
@@ -445,4 +464,4 @@ private static string GenerateErrorModelName(string methodName) | |
"{0}ErrorModel", methodName); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While personally, I'd prefer using the
1TBS
, the rest of the project uses the traditional ANSI-style braces, and I'd prefer to keep code changes in a single brace format.If you could tweak the braces to be consistent that'd be great.