Skip to content

Commit

Permalink
Merge pull request #65 from bigred8982/issues/64
Browse files Browse the repository at this point in the history
Thanks @bigred8982!
  • Loading branch information
Richard Beauchamp committed Feb 10, 2016
2 parents 93a9886 + 41a2c60 commit f581fc2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; Top-most EditorConfig file
root = true

; 4-column space indention
[*.cs]
indent_style = space
indent_size = 4
40 changes: 40 additions & 0 deletions Swashbuckle.OData.Tests/Fixtures/GetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,46 @@ public async Task It_has_all_optional_odata_query_parameters()
}
}

[Test]
public async Task It_has_collection_odata_query_parameters()
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(CustomersController))))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);

// Act
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1");

// Assert
PathItem pathItem;
swaggerDocument.paths.TryGetValue("/odata/Customers", out pathItem);
pathItem.get.parameters.Where(parameter => parameter.name.StartsWith("$")).Should().HaveCount(7);

await ValidationUtils.ValidateSwaggerJson();
}
}

[Test]
public async Task It_has_single_entity_odata_query_parameters()
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(CustomersController))))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);

// Act
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1");

// Assert
PathItem pathItem;
swaggerDocument.paths.TryGetValue("/odata/Customers({Id})", out pathItem);
pathItem.get.parameters.Where(parameter => parameter.name.StartsWith("$")).Should().HaveCount(2);

await ValidationUtils.ValidateSwaggerJson();
}
}

[Test]
public async Task It_has_a_parameter_with_a_name_equal_to_the_path_name()
{
Expand Down
11 changes: 9 additions & 2 deletions Swashbuckle.OData/Descriptions/ODataSwaggerUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static PathItem CreateSwaggerPathForEntitySet(IEdmEntitySet entitySet, OD
.OperationId(entitySet.Name + "_Get")
.Description("Returns the EntitySet " + entitySet.Name)
.Tags(entitySet.Name)
.Parameters(AddQueryOptionParameters(new List<Parameter>()))
.Parameters(AddQueryOptionParametersForEntitySet(new List<Parameter>()))
.Parameters(AddRoutePrefixParameters(oDataRoute))
.Responses(new Dictionary<string, Response>().Response("200", "EntitySet " + entitySet.Name, entitySet.Type).DefaultErrorResponse()),
post = new Operation()
Expand Down Expand Up @@ -130,7 +130,7 @@ private static void SetSwaggerType(Parameter parameter, IHttpRouteConstraint rou
}
}

public static IList<Parameter> AddQueryOptionParameters(IList<Parameter> parameterList)
public static IList<Parameter> AddQueryOptionParametersForEntitySet(IList<Parameter> parameterList)
{
return parameterList
.Parameter("$expand", "query", "Expands related entities inline.", "string", false)
Expand All @@ -142,6 +142,13 @@ public static IList<Parameter> AddQueryOptionParameters(IList<Parameter> paramet
.Parameter("$count", "query", "Includes a count of the matching results in the response.", "boolean", false);
}

public static IList<Parameter> AddQueryOptionParametersForEntity(IList<Parameter> parameterList)
{
return parameterList
.Parameter("$expand", "query", "Expands related entities inline.", "string", false)
.Parameter("$select", "query", "Selects which properties to include in the response.", "string", false);
}

/// <summary>
/// Create the Swagger path for the Edm entity.
/// </summary>
Expand Down
22 changes: 21 additions & 1 deletion Swashbuckle.OData/EnableQueryFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Web.OData;
using Swashbuckle.OData.Descriptions;
using Swashbuckle.Swagger;
using System.Web.Http;
using System;

namespace Swashbuckle.OData
{
Expand All @@ -21,7 +23,9 @@ public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescrip

if (HasEnableQueryAttribute(apiDescription) && !HasAnyQueryOptionParameters(operation))
{
operation.parameters = ODataSwaggerUtilities.AddQueryOptionParameters(operation.parameters ?? new List<Parameter>());
operation.parameters = ReturnsCollection(apiDescription)
? ODataSwaggerUtilities.AddQueryOptionParametersForEntitySet(operation.parameters ?? new List<Parameter>())
: ODataSwaggerUtilities.AddQueryOptionParametersForEntity(operation.parameters ?? new List<Parameter>());
}
}

Expand All @@ -36,5 +40,21 @@ private static bool HasEnableQueryAttribute(ApiDescription apiDescription)
Contract.Assume(httpActionDescriptor != null);
return httpActionDescriptor.GetCustomAttributes<EnableQueryAttribute>().Any();
}

private static bool ReturnsCollection(ApiDescription apiDescription)
{
var httpActionDescriptor = apiDescription.ActionDescriptor;
Contract.Assume(httpActionDescriptor != null);

Type returnType = httpActionDescriptor.ReturnType;

var responseTypeAttr = httpActionDescriptor.GetCustomAttributes<ResponseTypeAttribute>().FirstOrDefault();
if (responseTypeAttr != null)
returnType = responseTypeAttr.ResponseType;

return returnType.IsCollection();
}


}
}

0 comments on commit f581fc2

Please sign in to comment.