Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion API/Controller/Device/Pair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,34 @@ public sealed partial class DeviceController
[AllowAnonymous]
[MapToApiVersion("1")]
[HttpGet("pair/{pairCode}", Name = "Pair")]
[HttpGet("~/{version:apiVersion}/pair/{pairCode}", Name = "Pair_DEPRECATED")] // Backwards compatibility
[EndpointName("PairDeviceByCode")]
[EnableRateLimiting("auth")]
[ProducesResponseType<LegacyDataResponse<string>>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status404NotFound, MediaTypeNames.Application.ProblemJson)] // PairCodeNotFound
public async Task<IActionResult> Pair([FromRoute] string pairCode)
{
return await PairInternal(pairCode);
}

/// <summary>
/// Pair a device with a pair code.
/// </summary>
/// <param name="pairCode">The pair code to pair with.</param>
/// <response code="200">Successfully assigned LCG node</response>
/// <response code="404">No such pair code exists</response>
[AllowAnonymous]
[MapToApiVersion("1")]
[HttpGet("~/{version:apiVersion}/pair/{pairCode}", Name = "PairDeviceByCode_DEPRECATED")] // Backwards compatibility
[EndpointName("PairDeviceByCode_DEPRECATED")]
[EnableRateLimiting("auth")]
[ProducesResponseType<LegacyDataResponse<string>>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status404NotFound, MediaTypeNames.Application.ProblemJson)] // PairCodeNotFound
public async Task<IActionResult> PairDeprecated([FromRoute] string pairCode)
{
return await PairInternal(pairCode);
}

public async Task<IActionResult> PairInternal([FromRoute] string pairCode)
{
var devicePairs = _redis.RedisCollection<DevicePair>();

Expand Down
2 changes: 1 addition & 1 deletion Common/OpenAPI/DocumentDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static Func<OpenApiDocument, OpenApiDocumentTransformerContext, Cancellat

document.Info = new OpenApiInfo
{
Title = "OpenShock API",
Title = "OpenShock.API",
// Summary = ...
// Description = ...
Version = version,
Expand Down
35 changes: 35 additions & 0 deletions Common/OpenAPI/OpenApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,41 @@ public static IServiceCollection AddOpenApiExt<TProgram>(this WebApplicationBuil
options.OpenApiVersion = OpenApiSpecVersion.OpenApi3_1;
options.AddDocumentTransformer(DocumentDefaults.GetDocumentTransformer(
version: description.ApiVersion.ToString()));
options.CreateSchemaReferenceId = (type) =>
{
var defaultName = type.Type.Name;
var cleanName = defaultName
.Replace("[]", "Array")
.Replace("`1", "Of")
.Replace("`2", "OfTwo");
return cleanName;
};
options.AddOperationTransformer((operation, context, cancellationToken) =>
{
var actionDescriptor = context.Description.ActionDescriptor;

// Use endpoint name if available
var endpointName = actionDescriptor.EndpointMetadata
.OfType<EndpointNameMetadata>()
.FirstOrDefault()?.EndpointName;

if (!string.IsNullOrEmpty(endpointName))
{
operation.OperationId = endpointName;
return Task.CompletedTask;
}

// For controllers
var controller = actionDescriptor.RouteValues.TryGetValue("controller", out var ctrl) ? ctrl : null;
var action = actionDescriptor.RouteValues.TryGetValue("action", out var act) ? act : null;

if (!string.IsNullOrEmpty(controller) && !string.IsNullOrEmpty(action))
{
operation.OperationId = $"{controller}{action}";
}

return Task.CompletedTask;
});
});
}
}
Expand Down