Skip to content

Commit 5cdda49

Browse files
net0wellclaude
andcommitted
fix(dashboard): tipo de mídia dos arquivos enviados sem parâmetros e OpenAPI igual em todo SO (spec 007)
- Partes de arquivo com "text/html; charset=utf-8" eram recusadas pelo contrato (contentTypes compara o tipo de mídia); o painel passa só o tipo de mídia. - O documento OpenAPI servido normaliza fins de linha nas descrições. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ae0196d commit 5cdda49

3 files changed

Lines changed: 57 additions & 3 deletions

File tree

‎src/PythonScriptRunner.Dashboard/Api/DashboardOpenApi.cs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,57 @@ public static void Configure(OpenApiOptions options)
5252
});
5353
}
5454

55+
/// <summary>
56+
/// Descriptions come from XML comments and keep the line endings of the checkout (CRLF on Windows); the served document uses
57+
/// LF everywhere, so it is the same on every OS (its snapshot feeds the panel types).
58+
/// </summary>
59+
public static string NormalizeLineEndings(string json)
60+
{
61+
var document = JsonNode.Parse(json);
62+
Normalize(document);
63+
return document?.ToJsonString() ?? json;
64+
65+
static JsonNode? Fix(JsonNode? node) =>
66+
node is JsonValue value && value.TryGetValue<string>(out var text) && text.Contains('\r', StringComparison.Ordinal)
67+
? JsonValue.Create(text.ReplaceLineEndings("\n"))
68+
: null;
69+
70+
static void Normalize(JsonNode? node)
71+
{
72+
switch (node)
73+
{
74+
case JsonObject obj:
75+
foreach (var (name, child) in obj.ToList())
76+
{
77+
if (Fix(child) is { } fixedValue)
78+
{
79+
obj[name] = fixedValue;
80+
}
81+
else
82+
{
83+
Normalize(child);
84+
}
85+
}
86+
87+
break;
88+
case JsonArray array:
89+
for (var i = 0; i < array.Count; i++)
90+
{
91+
if (Fix(array[i]) is { } fixedValue)
92+
{
93+
array[i] = fixedValue;
94+
}
95+
else
96+
{
97+
Normalize(array[i]);
98+
}
99+
}
100+
101+
break;
102+
}
103+
}
104+
}
105+
55106
/// <summary>Strongly typed identifiers (<c>RunId</c>, <c>ArtifactId</c>...) are written as their GUID text.</summary>
56107
private static bool IsIdentifier(Type type) =>
57108
type is { IsValueType: true, Namespace: "PythonScriptRunner" } && type.Name.EndsWith("Id", StringComparison.Ordinal)

‎src/PythonScriptRunner.Dashboard/Api/RunInputs.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ private async Task ReadMultipartAsync(MultipartReader reader, PythonScriptRunner
149149
}
150150

151151
var safeName = Path.GetFileName(fileName.Replace('\\', '/'));
152-
var contentType = section.ContentType is { Length: > 0 } type ? type : PsrFile.GuessContentType(safeName);
152+
// Only the media type counts (browsers may add parameters such as charset to text files).
153+
var contentType = MediaTypeHeaderValue.TryParse(section.ContentType, out var media) && media.MediaType.HasValue
154+
? media.MediaType.Value!
155+
: PsrFile.GuessContentType(safeName);
153156
if (!_files.TryGetValue(name, out var list))
154157
{
155158
_files[name] = list = [];

‎src/PythonScriptRunner.Dashboard/DashboardEndpointRouteBuilderExtensions.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public static IEndpointConventionBuilder MapPythonScriptRunnerDashboard(this IEn
6868
{
6969
var provider = http.RequestServices.GetRequiredKeyedService<IOpenApiDocumentProvider>(DashboardOpenApi.Document);
7070
var document = await provider.GetOpenApiDocumentAsync(cancellationToken).ConfigureAwait(false);
71-
return TypedResults.Text(await document.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1, cancellationToken).ConfigureAwait(false),
72-
"application/json; charset=utf-8");
71+
var json = await document.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1, cancellationToken).ConfigureAwait(false);
72+
return TypedResults.Text(DashboardOpenApi.NormalizeLineEndings(json), "application/json; charset=utf-8");
7373
})
7474
.AllowAnonymous()
7575
.ExcludeFromDescription();

0 commit comments

Comments
 (0)