Skip to content

Commit

Permalink
Supports decimal parameters, keys and return types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Beauchamp committed Jan 30, 2016
1 parent 021812d commit 6583720
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Swashbuckle.OData.Nuget/Swashbuckle.OData.NuGet.nuproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
<Owners>Richard Beauchamp</Owners>
<Summary>Extends Swashbuckle with OData v4 support!</Summary>
<Description>Extends Swashbuckle with OData v4 support!</Description>
<ReleaseNotes>Supports OData actions that accept an array of Entities or Complex Types. Fixes #59, #60, #62.</ReleaseNotes>
<ReleaseNotes>Supports decimal parameters, keys and return types.</ReleaseNotes>
<ProjectUrl>https://github.com/rbeauchamp/Swashbuckle.OData</ProjectUrl>
<LicenseUrl>https://github.com/rbeauchamp/Swashbuckle.OData/blob/master/License.txt</LicenseUrl>
<Copyright>Copyright 2015</Copyright>
<Tags>Swashbuckle Swagger SwaggerUi OData Documentation Discovery Help WebApi AspNet AspNetWebApi Docs WebHost IIS</Tags>
<Version>2.12.5</Version>
<Version>2.13.0</Version>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Swashbuckle.OData\Swashbuckle.OData.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Swashbuckle.OData.Tests
public class DecimalParameterAndResponseTests
{
[Test]
public async Task It_supports_a_decimal_key()
public async Task It_supports_entity_with_a_decimal_key()
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(DecimalParametersController))))
{
Expand All @@ -44,6 +44,42 @@ public async Task It_supports_a_decimal_key()
}
}

[Test]
public async Task It_supports_functions_with_a_decimal_parameter()
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(DecimalParametersController))))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);
// Verify that the OData route in the test controller is valid
var result = await httpClient.GetJsonAsync<ODataResponse<decimal>>("/odata/DecimalParameters/Default.ResponseTest(param=2.5m)");
result.Value.Should().Be(2.5m);

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

// Assert
PathItem pathItem;
swaggerDocument.paths.TryGetValue("/odata/DecimalParameters/Default.ResponseTest(param={param})", out pathItem);
pathItem.Should().NotBeNull();
pathItem.get.Should().NotBeNull();
var getResponse = pathItem.get.responses.SingleOrDefault(response => response.Key == "200");
getResponse.Should().NotBeNull();
getResponse.Value.schema.@ref.Should().Be("#/definitions/ODataResponse[Decimal]");
swaggerDocument.definitions.Should().ContainKey("ODataResponse[Decimal]");
var responseSchema = swaggerDocument.definitions["ODataResponse[Decimal]"];
responseSchema.Should().NotBeNull();
responseSchema.properties.Should().NotBeNull();
responseSchema.properties.Should().ContainKey("@odata.context");
responseSchema.properties["@odata.context"].type.Should().Be("string");
responseSchema.properties["value"].type.Should().Be("number");
responseSchema.properties["value"].format.Should().Be("decimal");
responseSchema.properties["value"].items.Should().BeNull();

await ValidationUtils.ValidateSwaggerJson();
}
}

private static void Configuration(IAppBuilder appBuilder, Type targetController)
{
var config = appBuilder.GetStandardHttpConfig(targetController);
Expand Down
14 changes: 13 additions & 1 deletion Swashbuckle.OData/DefaultCompositionRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static SwaggerProviderOptions GetSwaggerProviderOptions(SwaggerDocsConfig
swaggerDocsConfig.GetFieldValue<bool>("_ignoreObsoleteActions"),
swaggerDocsConfig.GetFieldValue<Func<ApiDescription, string>>("_groupingKeySelector"),
swaggerDocsConfig.GetFieldValue<IComparer<string>>("_groupingKeyComparer"),
swaggerDocsConfig.GetFieldValue<IDictionary<Type, Func<Schema>>>("_customSchemaMappings"),
GetODataCustomSchemaMappings(swaggerDocsConfig),
swaggerDocsConfig.GetFieldValue<IList<Func<ISchemaFilter>>>("_schemaFilters", true).Select(factory => factory()),
swaggerDocsConfig.GetFieldValue<IList<Func<IModelFilter>>>("_modelFilters", true).Select(factory => factory()),
swaggerDocsConfig.GetFieldValue<bool>("_ignoreObsoleteProperties"),
Expand All @@ -86,6 +86,18 @@ public static SwaggerProviderOptions GetSwaggerProviderOptions(SwaggerDocsConfig
);
}

/// <summary>
/// Gets custom schema mappings that will only be applied to OData operations.
/// </summary>
/// <param name="swaggerDocsConfig">The swagger docs configuration.</param>
/// <returns></returns>
private static IDictionary<Type, Func<Schema>> GetODataCustomSchemaMappings(SwaggerDocsConfig swaggerDocsConfig)
{
var customSchemaMappings = swaggerDocsConfig.GetFieldValue<IDictionary<Type, Func<Schema>>>("_customSchemaMappings", true);
customSchemaMappings[typeof(decimal)] = () => new Schema { type = "number", format = "decimal" };
return customSchemaMappings;
}

/// <summary>
/// Gets operation filters that will only be applied to OData operations.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Swashbuckle.OData/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("2.12.5")]
[assembly: AssemblyInformationalVersion("2.13.0")]
4 changes: 4 additions & 0 deletions Swashbuckle.OData/SchemaRegistryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ private static bool IsResponseWithPrimiveTypeNotSupportedByJson(Type type, Messa
{
return true;
}
if (type == typeof(decimal))
{
return true;
}
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 2.12.5.{build}
version: 2.13.0.{build}

before_build:
- cmd: nuget restore
Expand Down

0 comments on commit 6583720

Please sign in to comment.