Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect the configured serializer in Ui.Playground #1121

Closed
Closed
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
31 changes: 22 additions & 9 deletions src/Ui.Playground/Internal/PlaygroundPageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ internal sealed class PlaygroundPageModel
private string? _playgroundCSHtml;

private readonly PlaygroundOptions _options;
private readonly IServiceProvider _services;

public PlaygroundPageModel(PlaygroundOptions options)
public PlaygroundPageModel(PlaygroundOptions options, IServiceProvider services)
{
_options = options;
_services = services;
}

public string Render()
Expand Down Expand Up @@ -60,19 +62,30 @@ public string Render()
return _playgroundCSHtml;
}

// https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
private static string StringEncode(string value) => value
.Replace("\\", "\\\\") // encode \ as \\
.Replace("<", "\\x3C") // encode < as \x3C -- so "<!--", "<script" and "</script" are handled correctly
.Replace("'", "\\'") // encode ' as \'
.Replace("\"", "\\\""); // encode " as \"

private static string JsonSerialize(object value)
private string JsonSerialize(object value)
{
if (_services.GetService(typeof(IGraphQLSerializer)) is IGraphQLSerializer serializer)
{
using var stream = new MemoryStream();
serializer.WriteAsync(stream, value).Wait();

stream.Position = 0;

using var reader = new StreamReader(stream, Encoding.UTF8);
return reader.ReadToEnd();
}
Comment on lines +67 to +76
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, GraphQL.NET provides a synchronous method as well:

Suggested change
if (_services.GetService(typeof(IGraphQLSerializer)) is IGraphQLSerializer serializer)
{
using var stream = new MemoryStream();
serializer.WriteAsync(stream, value).Wait();
stream.Position = 0;
using var reader = new StreamReader(stream, Encoding.UTF8);
return reader.ReadToEnd();
}
var serializer = _services.GetService<IGraphQLTextSerializer>();
if (serializer != null)
{
return serializer.Serialize(value);
}


#if NETSTANDARD2_0
return Newtonsoft.Json.JsonConvert.SerializeObject(value);
#else
return System.Text.Json.JsonSerializer.Serialize(value);
#endif
}

// https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
private static string StringEncode(string value) => value
.Replace("\\", "\\\\") // encode \ as \\
.Replace("<", "\\x3C") // encode < as \x3C -- so "<!--", "<script" and "</script" are handled correctly
.Replace("'", "\\'") // encode ' as \'
.Replace("\"", "\\\""); // encode " as \"
}
2 changes: 1 addition & 1 deletion src/Ui.Playground/PlaygroundMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Task Invoke(HttpContext httpContext)
httpContext.Response.ContentType = "text/html";
httpContext.Response.StatusCode = 200;

_pageModel ??= new PlaygroundPageModel(_options);
_pageModel ??= new PlaygroundPageModel(_options, httpContext.RequestServices);

byte[] data = Encoding.UTF8.GetBytes(_pageModel.Render());
return httpContext.Response.Body.WriteAsync(data, 0, data.Length);
Expand Down
4 changes: 4 additions & 0 deletions src/Ui.Playground/Ui.Playground.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<EmbeddedResource Include="Internal\playground.cshtml" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="GraphQL" Version="$(GraphQLVersion)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0'">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
Expand Down
Loading