Skip to content

Commit

Permalink
Configuration option now allows Navigation Properties to be included …
Browse files Browse the repository at this point in the history
…in the entity swagger models. Closes #58.
  • Loading branch information
Richard Beauchamp committed Feb 11, 2016
1 parent 9011093 commit 59db8c0
Show file tree
Hide file tree
Showing 15 changed files with 255 additions and 225 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ In `SwaggerConfig` configure the custom provider:
c.CustomProvider(defaultProvider => new ODataSwaggerProvider(defaultProvider, c));
```

### Custom Swagger Routes ###
### Include Navigation Properties in your entity swagger models ###

By default, OData does not get related entities unless you specify `$expand` on a navigation property.
Swashbuckle.OData tries to accurately reflect this behavior and therefore, by default, does not include
navigation properties in your entity swagger models. You can override this though by specifying:
```csharp
c.CustomProvider(defaultProvider => new ODataSwaggerProvider(defaultProvider, c).Configure(odataConfig =>
{
odataConfig.IncludeNavigationProperties();
}));
```

### Custom Swagger Routes ###

The following snippet demonstrates how to configure a custom swagger route such that it will appear in the Swagger UI:
```csharp
Expand Down
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>OData query parameters are limited to $expand and $select for APIs that return a single result.</ReleaseNotes>
<ReleaseNotes>Configuration option now allows Navigation Properties to be included in the entity swagger models.</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.15.0</Version>
<Version>2.16.0</Version>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Swashbuckle.OData\Swashbuckle.OData.csproj" />
Expand Down
7 changes: 6 additions & 1 deletion Swashbuckle.OData.Sample/App_Start/SwaggerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ public static void Register()
// Wrap the default SwaggerGenerator with additional behavior (e.g. caching) or provide an
// alternative implementation for ISwaggerProvider with the CustomProvider option.
//
c.CustomProvider(defaultProvider => new ODataSwaggerProvider(defaultProvider, c));
c.CustomProvider(defaultProvider => new ODataSwaggerProvider(defaultProvider, c).Configure(odataConfig =>
{
// Set this flag to include navigation properties in your entity swagger models
//
//odataConfig.IncludeNavigationProperties();
}));
})
.EnableSwaggerUi(c =>
{
Expand Down
12 changes: 6 additions & 6 deletions Swashbuckle.OData.Tests/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ public static class AppBuilderExtensions
{
public static HttpConfiguration GetStandardHttpConfig(this IAppBuilder appBuilder, params Type[] targetControllers)
{
return GetStandardHttpConfig(appBuilder, null, targetControllers);
return GetStandardHttpConfig(appBuilder, null, null, targetControllers);
}

public static HttpConfiguration GetStandardHttpConfig(this IAppBuilder appBuilder, Action<SwaggerDocsConfig> unitTestConfigs, params Type[] targetControllers)
public static HttpConfiguration GetStandardHttpConfig(this IAppBuilder appBuilder, Action<SwaggerDocsConfig> swaggerDocsConfig, Action<ODataSwaggerDocsConfig> odataSwaggerDocsConfig, params Type[] targetControllers)
{
var config = new HttpConfiguration
{
IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always
};
return ConfigureHttpConfig(appBuilder, config, unitTestConfigs, targetControllers);
return ConfigureHttpConfig(appBuilder, config, swaggerDocsConfig, odataSwaggerDocsConfig, targetControllers);
}

public static HttpConfiguration ConfigureHttpConfig(this IAppBuilder appBuilder, HttpConfiguration config, Action<SwaggerDocsConfig> unitTestConfigs, params Type[] targetControllers)
public static HttpConfiguration ConfigureHttpConfig(this IAppBuilder appBuilder, HttpConfiguration config, Action<SwaggerDocsConfig> swaggerDocsConfig, Action<ODataSwaggerDocsConfig> odataSwaggerDocsConfig, params Type[] targetControllers)
{
var server = new HttpServer(config);
appBuilder.UseWebApi(server);
Expand All @@ -38,10 +38,10 @@ public static HttpConfiguration ConfigureHttpConfig(this IAppBuilder appBuilder,
// Wrap the default SwaggerGenerator with additional behavior (e.g. caching) or provide an
// alternative implementation for ISwaggerProvider with the CustomProvider option.
//
c.CustomProvider(defaultProvider => new ODataSwaggerProvider(defaultProvider, c, config));
c.CustomProvider(defaultProvider => new ODataSwaggerProvider(defaultProvider, c, config).Configure(odataSwaggerDocsConfig));
// Apply test-specific configs
unitTestConfigs?.Invoke(c);
swaggerDocsConfig?.Invoke(c);
}).EnableSwaggerUi();

FormatterConfig.Register(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public static HttpConfiguration ConfigureWebApi(HttpConfiguration config)
return config;
}

private static HttpConfiguration ConfigureOData(IAppBuilder appBuilder, Type[] targetController, HttpConfiguration config, Action<SwaggerDocsConfig> unitTestConfigs)
private static HttpConfiguration ConfigureOData(IAppBuilder appBuilder, Type[] targetController, HttpConfiguration config, Action<SwaggerDocsConfig> swaggerDocsConfig)
{
config = appBuilder.ConfigureHttpConfig(config, unitTestConfigs, targetController);
config = appBuilder.ConfigureHttpConfig(config, swaggerDocsConfig, null, targetController);

config.MapODataServiceRoute("odata", "odata", GetEdmModel());

Expand Down
6 changes: 3 additions & 3 deletions Swashbuckle.OData.Tests/Fixtures/ODataSwaggerProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task It_applies_document_filters()
Action<SwaggerDocsConfig> config = c => c.DocumentFilter<ApplyNewHostName>();
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);

using (WebApp.Start(HttpClientUtils.BaseAddress, builder => Configuration(builder, unitTestConfigs: config)))
using (WebApp.Start(HttpClientUtils.BaseAddress, builder => Configuration(builder, swaggerDocsConfig: config)))
{
// Act
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1");
Expand Down Expand Up @@ -145,9 +145,9 @@ public async Task It_supports_both_webapi_and_odata_controllers()
}
}

private static void Configuration(IAppBuilder appBuilder, Type targetController = null, Action<SwaggerDocsConfig> unitTestConfigs = null)
private static void Configuration(IAppBuilder appBuilder, Type targetController = null, Action<SwaggerDocsConfig> swaggerDocsConfig = null)
{
var config = appBuilder.GetStandardHttpConfig(unitTestConfigs, targetController);
var config = appBuilder.GetStandardHttpConfig(swaggerDocsConfig, null, targetController);

var controllerSelector = new UnitTestODataVersionControllerSelector(config, targetController);
config.Services.Replace(typeof(IHttpControllerSelector), controllerSelector);
Expand Down
15 changes: 7 additions & 8 deletions Swashbuckle.OData.Tests/Fixtures/SchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.Owin.Hosting;
using NUnit.Framework;
using Owin;
using Swashbuckle.Application;
using Swashbuckle.Swagger;
using SwashbuckleODataSample;
using SwashbuckleODataSample.ApiControllers;
Expand All @@ -20,9 +19,9 @@ namespace Swashbuckle.OData.Tests
public class SchemaTests
{
[Test]
public async Task LimitSchemaGraphToTopLevelEntity_can_be_disabled()
public async Task Schema_can_be_configured_to_display_nested_reference_types_for_odata_controllers()
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(CustomersController), config => config.DisplayFullODataEntityGraph())))
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(CustomersController), oc => oc.IncludeNavigationProperties())))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);
Expand All @@ -31,15 +30,15 @@ public async Task LimitSchemaGraphToTopLevelEntity_can_be_disabled()
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1");

// Assert
swaggerDocument.definitions["Customer"].properties.ContainsKey("Orders").Should().BeFalse();
swaggerDocument.definitions["Order"].properties.ContainsKey("Customer").Should().BeFalse();
swaggerDocument.definitions["Customer"].properties.ContainsKey("Orders").Should().BeTrue();
swaggerDocument.definitions["Order"].properties.ContainsKey("Customer").Should().BeTrue();

await ValidationUtils.ValidateSwaggerJson();
}
}

[Test]
public async Task Schema_does_not_contain_nested_reference_types_for_odata_controllers()
public async Task By_default_the_schema_does_not_contain_nested_reference_types_for_odata_controllers()
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(CustomersController))))
{
Expand Down Expand Up @@ -75,9 +74,9 @@ public async Task Schema_contains_nested_reference_types_for_web_api_controllers
}
}

private static void Configuration(IAppBuilder appBuilder, Type targetController, Action<SwaggerDocsConfig> unitTestConfigs = null)
private static void Configuration(IAppBuilder appBuilder, Type targetController, Action<ODataSwaggerDocsConfig> odataSwaggerDocsConfig = null)
{
var config = appBuilder.GetStandardHttpConfig(targetController, unitTestConfigs);
var config = appBuilder.GetStandardHttpConfig(null, odataSwaggerDocsConfig, targetController);

WebApiConfig.Register(config);

Expand Down
146 changes: 0 additions & 146 deletions Swashbuckle.OData/DefaultCompositionRoot.cs

This file was deleted.

Loading

0 comments on commit 59db8c0

Please sign in to comment.