Skip to content

Commit 3eb59e0

Browse files
committed
Added quoted profile on API's error responses.
1 parent 86e46ab commit 3eb59e0

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/MakingSense.AspNetCore.HypermediaApi/ExceptionHandling/ApiErrorHandlerMiddleware.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,14 @@ private static void WriteResponseHeaders(Problem problem, HttpRequest request, H
161161
// otherwise return application/json + schema
162162
var acceptsProblemType = request.Headers[HeaderNames.Accept].Contains(PROBLEM_MEDIATYPE);
163163

164+
var keepUnquoted = request.Query.ContainsKey("keep-unquoted-profile");
165+
164166
response.OnStarting((o) =>
165167
{
166-
response.ContentType = acceptsProblemType ? PROBLEM_MEDIATYPE
167-
: response.ContentType + $"; profile={SchemaAttribute.Path}problem.json";
168+
if (acceptsProblemType)
169+
response.ContentType = PROBLEM_MEDIATYPE;
170+
else
171+
AddQuotedProfileToContentType(response, keepUnquoted);
168172
return Task.FromResult(0);
169173
}, null);
170174

@@ -177,5 +181,12 @@ private static void WriteResponseHeaders(Problem problem, HttpRequest request, H
177181
// Set problem status code
178182
response.StatusCode = problem.status;
179183
}
184+
185+
private static void AddQuotedProfileToContentType(HttpResponse response, bool keepUnquoted = false)
186+
{
187+
var profile = keepUnquoted ? SchemaAttribute.Path
188+
: $"\"{SchemaAttribute.Path?.Replace("\"", "\\\"")}\"";
189+
response.ContentType += $"; profile={profile}problem.json";
190+
}
180191
}
181192
}

0 commit comments

Comments
 (0)