-
Notifications
You must be signed in to change notification settings - Fork 780
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
275 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
test/TestApp.AspNetCore/Controllers/V1/ApiVersioningController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// <copyright file="ValuesController.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace TestApp.AspNetCore.Controllers.V1 | ||
{ | ||
[ApiController] | ||
[Route("api/v{version:apiVersion}/[controller]")] | ||
[ApiVersion("1.0")] | ||
public class ApiVersioningController : Controller | ||
{ | ||
// GET api/v1/apiVersion | ||
[HttpGet] | ||
[MapToApiVersion("1.0")] | ||
public string Get() | ||
{ | ||
return "version 1"; | ||
} | ||
|
||
// GET api/v1/apiVersion/42 | ||
[HttpGet("{id}")] | ||
[MapToApiVersion("1.0")] | ||
public string Get(int id) | ||
{ | ||
return $"version 1 (id = {id})"; | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
test/TestApp.AspNetCore/Controllers/V2/ApiVersioningController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// <copyright file="ValuesController.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace TestApp.AspNetCore.Controllers.V2 | ||
{ | ||
[ApiController] | ||
[Route("api/v{version:apiVersion}/[controller]")] | ||
[ApiVersion("2.0")] | ||
public class ApiVersioningController : Controller | ||
{ | ||
// GET api/v2/apiVersion | ||
[HttpGet] | ||
[MapToApiVersion("2.0")] | ||
public string Get() | ||
{ | ||
return "version 2"; | ||
} | ||
|
||
// GET api/v2/apiVersion/42 | ||
[HttpGet("{id}")] | ||
[MapToApiVersion("2.0")] | ||
public string Get(int id) | ||
{ | ||
return $"version 2 (id = {id})"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
test/TestApp.AspNetCore/Swagger/ConfigureSwaggerOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// <copyright file="ConfigureSwaggerOptions.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace TestApp.AspNetCore.Swagger | ||
{ | ||
public sealed class ConfigureSwaggerOptions : IConfigureNamedOptions<SwaggerGenOptions> | ||
{ | ||
private readonly IApiVersionDescriptionProvider provider; | ||
|
||
public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider) | ||
{ | ||
this.provider = provider ?? throw new ArgumentNullException(nameof(provider)); | ||
} | ||
|
||
public void Configure(string? name, SwaggerGenOptions options) | ||
{ | ||
this.Configure(options); | ||
} | ||
|
||
public void Configure(SwaggerGenOptions options) | ||
{ | ||
if (options is null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
// Add swagger document for every API version discovered | ||
foreach (var description in this.provider.ApiVersionDescriptions) | ||
{ | ||
options.SwaggerDoc(description.GroupName, CreateApiVersion(description)); | ||
} | ||
} | ||
|
||
private static OpenApiInfo CreateApiVersion(ApiVersionDescription description) | ||
{ | ||
return new OpenApiInfo | ||
{ | ||
Title = "TestApp.AspNetCore", | ||
Version = description.ApiVersion.ToString(), | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters