Skip to content

Commit

Permalink
Fix OdsContext handling in Swagger generation (#832)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjaksn authored Oct 5, 2023
1 parent 7bb24a3 commit 271edc5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class OpenApiMetadataRequest

public string CompositeCategoryName { get; set; }

public string SchoolYear { get; set; }
public string OdsContext { get; set; }

public string InstanceId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ public bool TryGetSwaggerDocument(HttpRequest request, out string document)
return false;
}

document = GetMetadataForContent(openApiContent, request, openApiMetadataRequest.SchoolYear, openApiMetadataRequest.InstanceId, openApiMetadataRequest.TenantIdentifierFromRoute);
document = GetMetadataForContent(openApiContent, request, openApiMetadataRequest.OdsContext, openApiMetadataRequest.InstanceId, openApiMetadataRequest.TenantIdentifierFromRoute);

return true;
}

private string GetMetadataForContent(OpenApiContent content, HttpRequest request, string schoolYear, string instanceId, string tenantIdentifier)
private string GetMetadataForContent(OpenApiContent content, HttpRequest request, string odsContextName, string instanceId, string tenantIdentifier)
{

var schoolYearRouteValue = string.IsNullOrEmpty(schoolYear) ? string.Empty : $"{schoolYear}/";
var odsContextRouteValue = string.IsNullOrEmpty(odsContextName) ? string.Empty : $"{odsContextName}/";
var instanceIdRouteValue = string.IsNullOrEmpty(instanceId) ? string.Empty : $"{instanceId}/";
var tenantIdentifierRouteValue = string.IsNullOrEmpty(tenantIdentifier) ? string.Empty : $"{tenantIdentifier}/";

string basePath = request.PathBase.Value.EnsureSuffixApplied("/") + tenantIdentifierRouteValue + schoolYearRouteValue + content.BasePath.EnsureSuffixApplied("/") + instanceIdRouteValue;
string basePath = request.PathBase.Value.EnsureSuffixApplied("/") + tenantIdentifierRouteValue + odsContextRouteValue + content.BasePath.EnsureSuffixApplied("/") + instanceIdRouteValue;

return content.Metadata
.Replace("%HOST%", Host())
Expand All @@ -87,7 +87,7 @@ private string GetMetadataForContent(OpenApiContent content, HttpRequest request

string TokenUrl() {
var rootUrl = request.RootUrl(this._reverseProxySettings);
return $"{rootUrl}/" + tenantIdentifierRouteValue + schoolYearRouteValue + $"{instanceId}oauth/token";
return $"{rootUrl}/" + tenantIdentifierRouteValue + odsContextRouteValue + $"{instanceId}oauth/token";
}

string Host() => $"{request.Host(this._reverseProxySettings)}:{request.Port(this._reverseProxySettings)}";
Expand All @@ -99,6 +99,8 @@ private OpenApiMetadataRequest CreateOpenApiMetadataRequest(string path)
// this is less effort that rewriting the open api metadata cache.
var openApiMetadataRequest = new OpenApiMetadataRequest();

var odsContextVariableName = _odsContextRoutePath?.TrimStart('{').TrimEnd('}');

var matcher = new RouteMatcher();

foreach (var routeInformation in _routeInformations)
Expand Down Expand Up @@ -129,9 +131,9 @@ private OpenApiMetadataRequest CreateOpenApiMetadataRequest(string path)
}
}

if (values.ContainsKey("schoolYear"))
if (odsContextVariableName != null && values.ContainsKey(odsContextVariableName))
{
openApiMetadataRequest.SchoolYear = values["schoolYear"]
openApiMetadataRequest.OdsContext = values[odsContextVariableName]
.ToString();
}

Expand Down

0 comments on commit 271edc5

Please sign in to comment.