Skip to content
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

Openapi stuff #26

Merged
merged 13 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft.azure/autorest.modeler",
"version": "2.1.0",
"version": "2.2.0",
"description": "The modeler extension for classic generators in AutoRest.",
"scripts": {
"start": "dotnet src/bin/netcoreapp2.0/autorest.modeler.dll --server",
Expand Down Expand Up @@ -46,4 +46,4 @@
"dependencies": {
"dotnet-2.0.0": "^1.4.4"
}
}
}
17 changes: 17 additions & 0 deletions src/BuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ namespace AutoRest.Modeler
{
public static class BuilderExtensions
{
/// <summary>
/// Removes #/components/{component}/ or url#/components/{component} from the reference path.
/// </summary>
private static string StripSomeComponentPath(string component, string reference)
{
var prefix = $"#/components/{component}/";
if (reference != null && reference.Contains(prefix))
{
reference = reference.Substring(reference.IndexOf(prefix, StringComparison.OrdinalIgnoreCase) + prefix.Length);
}
return reference;
}

public static string StripComponentsParameterPath(this string reference) => StripSomeComponentPath("parameters", reference);
public static string StripComponentsRequestBodyPath(this string reference) => StripSomeComponentPath("requestBodies", reference);
public static string StripComponentsSchemaPath(this string reference) => StripSomeComponentPath("schemas", reference);

/// <summary>
/// A schema represents a primitive type if it's not an object or it represents a dictionary
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/CollectionFormatBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public static StringBuilder OnBuildMethodParameter(Method method,

bool hasCollectionFormat = currentSwaggerParam.CollectionFormat != CollectionFormat.None;

if (currentSwaggerParam.Type == DataType.Array && !hasCollectionFormat)
if (currentSwaggerParam.Schema?.Type == DataType.Array && !hasCollectionFormat)
{
// If the parameter type is array default the collectionFormat to csv
currentSwaggerParam.CollectionFormat = CollectionFormat.Csv;
currentSwaggerParam.Style = ParameterStyle.Form;
}

if (hasCollectionFormat)
Expand Down
7 changes: 3 additions & 4 deletions src/JsonConverters/ResponseRefConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ public override object ReadJson(JsonReader reader, System.Type objectType, objec
{
referencePath = jo.GetValue("$ref", StringComparison.Ordinal).ToString();
// Shorthand notation
if (!referencePath.StartsWith("#/responses", StringComparison.Ordinal))
if (!referencePath.StartsWith("#/components/responses", StringComparison.Ordinal))
{
referencePath = "#/responses/" + referencePath;
referencePath = "#/components/responses/" + referencePath;
}
jo = Document.SelectToken(referencePath.Replace("#/", "").Replace("/", ".")) as JObject;
}

OperationResponse swaggerResponse = JsonConvert.DeserializeObject<OperationResponse>(jo.ToString(),
GetSettings(serializer));
OperationResponse swaggerResponse = JsonConvert.DeserializeObject<OperationResponse>(jo.ToString(), GetSettings(serializer));
return swaggerResponse;
}
}
Expand Down
45 changes: 0 additions & 45 deletions src/JsonConverters/SchemaRequiredItemConverter.cs

This file was deleted.

30 changes: 30 additions & 0 deletions src/Model/Components.cs
Original file line number Diff line number Diff line change
@@ -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.Collections.Generic;

namespace AutoRest.Modeler.Model
{
public class Components : SwaggerBase
{
public Components()
{
Schemas = new Dictionary<string, Schema>();
Parameters = new Dictionary<string, SwaggerParameter>();
RequestBodies = new Dictionary<string, RequestBody>();
Responses = new Dictionary<string, OperationResponse>();
SecuritySchemes = new Dictionary<string, SecurityDefinition>();
}

public Dictionary<string, Schema> Schemas { get; set; }

public Dictionary<string, SwaggerParameter> Parameters { get; set; }

public Dictionary<string, RequestBody> RequestBodies { get; set; }

public Dictionary<string, OperationResponse> Responses { get; set; }

public Dictionary<string, SecurityDefinition> SecuritySchemes { get; set; }

}
}
15 changes: 15 additions & 0 deletions src/Model/Discriminator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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;

namespace AutoRest.Modeler.Model
{
public class Discriminator
{
public string PropertyName { get; set; }

// TODO: translate x-ms-discriminator-value to this! Completely ignored so far.
public Dictionary<string, string> Mapping { get; set; }
}
}
17 changes: 15 additions & 2 deletions src/Model/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Newtonsoft.Json;

namespace AutoRest.Modeler.Model
{
/// <summary>
/// Swagger header object.
/// </summary>
public class Header : SwaggerObject
{
public class Header : SwaggerBase
{
public string Description { get; set; }

[JsonProperty(PropertyName = "$ref")]
public string Reference { get; set; }

[JsonProperty(PropertyName = "required")]
public virtual bool IsRequired { get; set; }

/// <summary>
/// The schema defining the type used for the body parameter.
/// </summary>
public Schema Schema { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/Model/MediaTypeObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.


namespace AutoRest.Modeler.Model
{
public class MediaTypeObject : SwaggerBase
{
public Schema Schema { get; set; }
}
}
49 changes: 38 additions & 11 deletions src/Model/Operation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Collections.Generic;
using AutoRest.Core.Utilities;
using Newtonsoft.Json;

namespace AutoRest.Modeler.Model
{
Expand All @@ -17,8 +18,6 @@ public class Operation : SwaggerBase

public Operation()
{
Consumes = new List<string>();
Produces = new List<string>();
}

/// <summary>
Expand Down Expand Up @@ -50,22 +49,50 @@ public string Description
/// </summary>
public ExternalDoc ExternalDocs { get; set; }

/// <summary>
/// A list of MIME types the operation can consume.
/// </summary>
public IList<string> Consumes { get; set; }
// TODO: fix/remove
public IEnumerable<string> GetConsumes(Dictionary<string, RequestBody> requestBodies)
{
var body = RequestBody;
if (body?.Reference != null)
{
body = requestBodies[body.Reference.StripComponentsRequestBodyPath()];
}
var result = body?.Content?.Keys.ToList();
if (result == null || result.Count == 0) return new List<string> { "application/json" };
return result;
}

/// <summary>
/// A list of MIME types the operation can produce.
/// </summary>
public IList<string> Produces { get; set; }
// TODO: fix/remove
public IEnumerable<string> GetProduces()
{
var result = Responses?.Values.SelectMany(r => r.Content?.Keys ?? Enumerable.Empty<string>()).Distinct().ToList();
if (result == null || result.Count == 0 || result.Count == 1 && result[0] == "*/*") return new List<string> { "application/json" };
return result;
}

[JsonIgnore]
IList<SwaggerParameter> _parameters;
/// <summary>
/// A list of parameters that are applicable for this operation.
/// If a parameter is already defined at the Path Item, the
/// new definition will override it, but can never remove it.
/// </summary>
public IList<SwaggerParameter> Parameters { get; set; }
[JsonProperty(PropertyName = "parameters")]
public SwaggerParameter[] Parameters
{
get
{
var result = _parameters?.ToList() ?? new List<SwaggerParameter>();
if (RequestBody != null)
{
result.InsertRange(Math.Min(Extensions.Get<int>("x-ms-requestBody-index") ?? 0, result.Count), RequestBody.AsParameters());
}
return result.ToArray();
}
set => _parameters = value.Where(v => v.In != ParameterLocation.Body).ToList();
} // TODO: not like this...

public RequestBody RequestBody { get; set; }

/// <summary>
/// The list of possible responses as they are returned from executing this operation.
Expand Down
3 changes: 2 additions & 1 deletion src/Model/ParameterLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum ParameterLocation
Header,
Path,
FormData,
Body
Body,
Cookie
}
}
18 changes: 18 additions & 0 deletions src/Model/ParameterStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.


namespace AutoRest.Modeler.Model
{
public enum ParameterStyle
{
Matrix,
Label,
Form,
Simple,
SpaceDelimited,
PipeDelimited,
DeepObject,
TabDelimited // FAKE
}
}
71 changes: 71 additions & 0 deletions src/Model/RequestBody.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using AutoRest.Core.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

namespace AutoRest.Modeler.Model
{
/// <summary>
/// Describes a single response from an API Operation.
/// </summary>
public class RequestBody : SwaggerBase
{
private string _description;

public string Description
{
get { return _description; }
set { _description = value.StripControlCharacters(); }
}

[JsonProperty(PropertyName = "$ref")]
public string Reference { get; set; }

// TODO: get rid of this
private IEnumerable<SwaggerParameter> asParamCache = null;
public IEnumerable<SwaggerParameter> AsParameters()
{
if (asParamCache == null)
{
var isFormData = Content?.Keys?.FirstOrDefault() == "multipart/form-data" && Content.Values.First().Schema != null;
if (isFormData) // => in: form-data
{
var schema = Content.Values.First().Schema;
asParamCache = schema.Properties.Select(prop =>
new SwaggerParameter
{
Description = prop.Value.Description,
In = ParameterLocation.FormData,
Name = prop.Key,
IsRequired = schema.Required.Contains(prop.Key),
Schema = prop.Value,
Extensions = schema.Extensions
});
}
else // => in: body
{
var p = new SwaggerParameter
{
Description = Description,
In = ParameterLocation.Body,
Name = Extensions.GetValue<string>("x-ms-requestBody-name") ?? "body",
IsRequired = Required,
Schema = Content?.Values.FirstOrDefault()?.Schema,
Reference = Reference,
Extensions = Extensions
};
asParamCache = new [] { p };
}
}
return asParamCache;
}

public Dictionary<string, MediaTypeObject> Content { get; set; }

public bool Required { get; set; }
}
}
Loading