Skip to content

Commit 97f8071

Browse files
Chris Martinezcommonsensesoftware
Chris Martinez
authored andcommitted
Updated OData swagger example
1 parent 13ee98a commit 97f8071

16 files changed

+193
-43
lines changed

samples/webapi/SwaggerODataWebApiSample/Models/ApiVersions.cs renamed to samples/webapi/SwaggerODataWebApiSample/Configuration/ApiVersions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Microsoft.Examples.Models
1+
namespace Microsoft.Examples.Configuration
22
{
33
using Microsoft.Web.Http;
44
using System;

samples/webapi/SwaggerODataWebApiSample/Configuration/OrderModelConfiguration.cs

+11-8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
{
33
using Microsoft.Web.Http;
44
using Microsoft.Web.OData.Builder;
5-
using Models;
65
using System.Web.OData.Builder;
76

87
/// <summary>
9-
/// Represents the model configuration for <see cref="Order">orders</see>.
8+
/// Represents the model configuration for orders.
109
/// </summary>
1110
public class OrderModelConfiguration : IModelConfiguration
1211
{
@@ -17,13 +16,17 @@ public class OrderModelConfiguration : IModelConfiguration
1716
/// <param name="apiVersion">The <see cref="ApiVersion">API version</see> associated with the <paramref name="builder"/>.</param>
1817
public void Apply( ODataModelBuilder builder, ApiVersion apiVersion )
1918
{
20-
var order = builder.EntitySet<Order>( "Orders" ).EntityType;
21-
22-
order.HasKey( o => o.Id );
23-
24-
if ( apiVersion < ApiVersions.V2 )
19+
if ( apiVersion <= ApiVersions.V1 )
20+
{
21+
builder.EntitySet<V1.Models.Order>( "Orders" ).EntityType.HasKey( o => o.Id );
22+
}
23+
else if ( apiVersion == ApiVersions.V2 )
24+
{
25+
builder.EntitySet<V2.Models.Order>( "Orders" ).EntityType.HasKey( o => o.Id );
26+
}
27+
else if ( apiVersion == ApiVersions.V3 )
2528
{
26-
order.Ignore( o => o.EffectiveDate );
29+
builder.EntitySet<V3.Models.Order>( "Orders" ).EntityType.HasKey( o => o.Id );
2730
}
2831
}
2932
}

samples/webapi/SwaggerODataWebApiSample/Configuration/PersonModelConfiguration.cs

+9-11
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
{
33
using Microsoft.Web.Http;
44
using Microsoft.Web.OData.Builder;
5-
using Models;
65
using System.Web.OData.Builder;
76

87
/// <summary>
9-
/// Represents the model configuration for <see cref="Person">people</see>.
8+
/// Represents the model configuration for people.
109
/// </summary>
1110
public class PersonModelConfiguration : IModelConfiguration
1211
{
@@ -17,18 +16,17 @@ public class PersonModelConfiguration : IModelConfiguration
1716
/// <param name="apiVersion">The <see cref="ApiVersion">API version</see> associated with the <paramref name="builder"/>.</param>
1817
public void Apply( ODataModelBuilder builder, ApiVersion apiVersion )
1918
{
20-
var person = builder.EntitySet<Person>( "People" ).EntityType;
21-
22-
person.HasKey( p => p.Id );
23-
24-
if ( apiVersion < ApiVersions.V3 )
19+
if ( apiVersion <= ApiVersions.V1 )
2520
{
26-
person.Ignore( p => p.Phone );
21+
builder.EntitySet<V1.Models.Person>( "People" ).EntityType.HasKey( p => p.Id );
2722
}
28-
29-
if ( apiVersion < ApiVersions.V2 )
23+
else if ( apiVersion == ApiVersions.V2 )
24+
{
25+
builder.EntitySet<V2.Models.Person>( "People" ).EntityType.HasKey( p => p.Id );
26+
}
27+
else if ( apiVersion == ApiVersions.V3 )
3028
{
31-
person.Ignore( p => p.Email );
29+
builder.EntitySet<V3.Models.Person>( "People" ).EntityType.HasKey( p => p.Id );
3230
}
3331
}
3432
}

samples/webapi/SwaggerODataWebApiSample/SwaggerODataWebApiSample.csproj

+14-9
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,23 @@
128128
<ItemGroup>
129129
<Compile Include="CaseInsensitiveODataUriResolver.cs" />
130130
<Compile Include="ImplicitApiVersionParameter.cs" />
131-
<Compile Include="Models\ApiVersions.cs" />
131+
<Compile Include="Configuration\ApiVersions.cs" />
132132
<Compile Include="Configuration\OrderModelConfiguration.cs" />
133133
<Compile Include="Configuration\PersonModelConfiguration.cs" />
134134
<Compile Include="Properties\AssemblyInfo.cs" />
135135
<Compile Include="Startup.cs" />
136-
<Compile Include="V1\OrdersController.cs" />
137-
<Compile Include="V1\PeopleController.cs" />
138-
<Compile Include="V2\OrdersController.cs" />
139-
<Compile Include="V2\PeopleController.cs" />
140-
<Compile Include="V3\OrdersController.cs" />
141-
<Compile Include="V3\PeopleController.cs" />
142-
<Compile Include="Models\Order.cs" />
143-
<Compile Include="Models\Person.cs" />
136+
<Compile Include="V1\Models\Order.cs" />
137+
<Compile Include="V1\Models\Person.cs" />
138+
<Compile Include="V1\Controllers\OrdersController.cs" />
139+
<Compile Include="V1\Controllers\PeopleController.cs" />
140+
<Compile Include="V2\Models\Order.cs" />
141+
<Compile Include="V2\Models\Person.cs" />
142+
<Compile Include="V2\Controllers\OrdersController.cs" />
143+
<Compile Include="V2\Controllers\PeopleController.cs" />
144+
<Compile Include="V3\Models\Order.cs" />
145+
<Compile Include="V3\Models\Person.cs" />
146+
<Compile Include="V3\Controllers\OrdersController.cs" />
147+
<Compile Include="V3\Controllers\PeopleController.cs" />
144148
</ItemGroup>
145149
<ItemGroup>
146150
<ProjectReference Include="..\..\..\src\Microsoft.AspNet.OData.Versioning.ApiExplorer\Microsoft.AspNet.OData.Versioning.ApiExplorer.csproj">
@@ -160,6 +164,7 @@
160164
<Name>Microsoft.AspNet.WebApi.Versioning</Name>
161165
</ProjectReference>
162166
</ItemGroup>
167+
<ItemGroup />
163168
<PropertyGroup>
164169
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
165170
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

samples/webapi/SwaggerODataWebApiSample/V1/PeopleController.cs renamed to samples/webapi/SwaggerODataWebApiSample/V1/Controllers/PeopleController.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Microsoft.Examples.V1.Controllers
22
{
33
using Microsoft.Web.Http;
4+
using Microsoft.Web.Http.Description;
45
using Models;
56
using System.Web.Http;
67
using System.Web.Http.Description;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace Microsoft.Examples.V1.Models
2+
{
3+
using System;
4+
using System.ComponentModel.DataAnnotations;
5+
6+
/// <summary>
7+
/// Represents an order.
8+
/// </summary>
9+
public class Order
10+
{
11+
/// <summary>
12+
/// Gets or sets the unique identifier for the order.
13+
/// </summary>
14+
/// <value>The order's unique identifier.</value>
15+
public int Id { get; set; }
16+
17+
/// <summary>
18+
/// Gets or sets the date and time when the order was created.
19+
/// </summary>
20+
/// <value>The order's creation date.</value>
21+
public DateTimeOffset CreatedDate { get; set; } = DateTimeOffset.Now;
22+
23+
/// <summary>
24+
/// Gets or sets the name of the ordering customer.
25+
/// </summary>
26+
/// <value>The name of the customer that placed the order.</value>
27+
[Required]
28+
public string Customer { get; set; }
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace Microsoft.Examples.V1.Models
2+
{
3+
using System;
4+
using System.ComponentModel.DataAnnotations;
5+
6+
/// <summary>
7+
/// Represents a person.
8+
/// </summary>
9+
public class Person
10+
{
11+
/// <summary>
12+
/// Gets or sets the unique identifier for a person.
13+
/// </summary>
14+
/// <value>The person's unique identifier.</value>
15+
public int Id { get; set; }
16+
17+
/// <summary>
18+
/// Gets or sets the first name of a person.
19+
/// </summary>
20+
/// <value>The person's first name.</value>
21+
[Required]
22+
[StringLength( 25 )]
23+
public string FirstName { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets the last name of a person.
27+
/// </summary>
28+
/// <value>The person's last name.</value>
29+
[Required]
30+
[StringLength( 25 )]
31+
public string LastName { get; set; }
32+
}
33+
}

samples/webapi/SwaggerODataWebApiSample/V2/OrdersController.cs renamed to samples/webapi/SwaggerODataWebApiSample/V2/Controllers/OrdersController.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Microsoft.Examples.V2.Controllers
22
{
33
using Microsoft.Web.Http;
4+
using Microsoft.Web.Http.Description;
45
using Models;
56
using System;
67
using System.Collections.Generic;
@@ -23,7 +24,7 @@ public class OrdersController : ODataController
2324
/// <response code="200">The successfully retrieved orders.</response>
2425
[HttpGet]
2526
[ODataRoute]
26-
[ResponseType( typeof( IEnumerable<Order> ) )]
27+
[ResponseType( typeof( ODataValue<IEnumerable<Order>> ) )]
2728
public IHttpActionResult Get()
2829
{
2930
var orders = new[]

samples/webapi/SwaggerODataWebApiSample/V2/PeopleController.cs renamed to samples/webapi/SwaggerODataWebApiSample/V2/Controllers/PeopleController.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Microsoft.Examples.V2.Controllers
22
{
33
using Microsoft.Web.Http;
4+
using Microsoft.Web.Http.Description;
45
using Models;
56
using System.Collections.Generic;
67
using System.Web.Http;
@@ -22,7 +23,7 @@ public class PeopleController : ODataController
2223
/// <response code="200">The successfully retrieved people.</response>
2324
[HttpGet]
2425
[ODataRoute]
25-
[ResponseType( typeof( IEnumerable<Person> ) )]
26+
[ResponseType( typeof( ODataValue<IEnumerable<Person>> ) )]
2627
public IHttpActionResult Get()
2728
{
2829
var people = new[]

samples/webapi/SwaggerODataWebApiSample/Models/Order.cs renamed to samples/webapi/SwaggerODataWebApiSample/V2/Models/Order.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Microsoft.Examples.Models
1+
namespace Microsoft.Examples.V2.Models
22
{
33
using System;
44
using System.ComponentModel.DataAnnotations;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace Microsoft.Examples.V2.Models
2+
{
3+
using System;
4+
using System.ComponentModel.DataAnnotations;
5+
6+
/// <summary>
7+
/// Represents a person.
8+
/// </summary>
9+
public class Person
10+
{
11+
/// <summary>
12+
/// Gets or sets the unique identifier for a person.
13+
/// </summary>
14+
/// <value>The person's unique identifier.</value>
15+
public int Id { get; set; }
16+
17+
/// <summary>
18+
/// Gets or sets the first name of a person.
19+
/// </summary>
20+
/// <value>The person's first name.</value>
21+
[Required]
22+
[StringLength( 25 )]
23+
public string FirstName { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets the last name of a person.
27+
/// </summary>
28+
/// <value>The person's last name.</value>
29+
[Required]
30+
[StringLength( 25 )]
31+
public string LastName { get; set; }
32+
33+
/// <summary>
34+
/// Gets or sets the email address for a person.
35+
/// </summary>
36+
/// <value>The person's email address.</value>
37+
public string Email { get; set; }
38+
}
39+
}

samples/webapi/SwaggerODataWebApiSample/V3/OrdersController.cs renamed to samples/webapi/SwaggerODataWebApiSample/V3/Controllers/OrdersController.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Microsoft.Examples.V3.Controllers
22
{
33
using Microsoft.Web.Http;
4+
using Microsoft.Web.Http.Description;
45
using Models;
56
using System;
67
using System.Collections.Generic;
@@ -25,7 +26,7 @@ public class OrdersController : ODataController
2526
/// <response code="400">The order is invalid.</response>
2627
[HttpGet]
2728
[ODataRoute]
28-
[ResponseType( typeof( IEnumerable<Order> ) )]
29+
[ResponseType( typeof( ODataValue<IEnumerable<Order>> ) )]
2930
public IHttpActionResult Get()
3031
{
3132
var orders = new[]
@@ -76,9 +77,10 @@ public IHttpActionResult Post( [FromBody] Order order )
7677
/// Cancels an order.
7778
/// </summary>
7879
/// <param name="id">The order to cancel.</param>
80+
/// <param name="suspendOnly">Indicates if the order should only be suspended.</param>
7981
/// <returns>None</returns>
8082
[HttpDelete]
8183
[ODataRoute( "({id})" )]
82-
public IHttpActionResult Delete( int id ) => StatusCode( NoContent );
84+
public IHttpActionResult Delete( int id, bool suspendOnly ) => StatusCode( NoContent );
8385
}
8486
}

samples/webapi/SwaggerODataWebApiSample/V3/PeopleController.cs renamed to samples/webapi/SwaggerODataWebApiSample/V3/Controllers/PeopleController.cs

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Microsoft.Examples.V3.Controllers
22
{
33
using Microsoft.Web.Http;
4+
using Microsoft.Web.Http.Description;
45
using Models;
56
using System.Collections.Generic;
67
using System.Web.Http;
@@ -22,7 +23,7 @@ public class PeopleController : ODataController
2223
/// <response code="200">The successfully retrieved people.</response>
2324
[HttpGet]
2425
[ODataRoute]
25-
[ResponseType( typeof( IEnumerable<Person> ) )]
26+
[ResponseType( typeof( ODataValue<IEnumerable<Person>> ) )]
2627
public IHttpActionResult Get()
2728
{
2829
var people = new[]
@@ -68,13 +69,13 @@ public IHttpActionResult Get()
6869
[ResponseType( typeof( Person ) )]
6970
public IHttpActionResult Get( int id ) =>
7071
Ok( new Person()
71-
{
72-
Id = id,
73-
FirstName = "John",
74-
LastName = "Doe",
75-
Email = "[email protected]",
76-
Phone = "555-987-1234"
77-
}
72+
{
73+
Id = id,
74+
FirstName = "John",
75+
LastName = "Doe",
76+
Email = "[email protected]",
77+
Phone = "555-987-1234"
78+
}
7879
);
7980

8081
/// <summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace Microsoft.Examples.V3.Models
2+
{
3+
using System;
4+
using System.ComponentModel.DataAnnotations;
5+
6+
/// <summary>
7+
/// Represents an order.
8+
/// </summary>
9+
public class Order
10+
{
11+
/// <summary>
12+
/// Gets or sets the unique identifier for the order.
13+
/// </summary>
14+
/// <value>The order's unique identifier.</value>
15+
public int Id { get; set; }
16+
17+
/// <summary>
18+
/// Gets or sets the date and time when the order was created.
19+
/// </summary>
20+
/// <value>The order's creation date.</value>
21+
public DateTimeOffset CreatedDate { get; set; } = DateTimeOffset.Now;
22+
23+
/// <summary>
24+
/// Gets or sets the date and time when the order becomes effective.
25+
/// </summary>
26+
/// <value>The order's effective date.</value>
27+
public DateTimeOffset EffectiveDate { get; set; } = DateTimeOffset.Now;
28+
29+
/// <summary>
30+
/// Gets or sets the name of the ordering customer.
31+
/// </summary>
32+
/// <value>The name of the customer that placed the order.</value>
33+
[Required]
34+
public string Customer { get; set; }
35+
}
36+
}

samples/webapi/SwaggerODataWebApiSample/Models/Person.cs renamed to samples/webapi/SwaggerODataWebApiSample/V3/Models/Person.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Microsoft.Examples.Models
1+
namespace Microsoft.Examples.V3.Models
22
{
33
using System;
44
using System.ComponentModel.DataAnnotations;

0 commit comments

Comments
 (0)