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: 23 additions & 2 deletions lib/graphql_query/schema/remote/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,7 @@ defmodule GraphqlQuery.Schema.Remote.Introspection do
# Deprecation
defp render_deprecated(%{"isDeprecated" => true, "deprecationReason" => reason})
when is_binary(reason) and reason != "" do
escaped = reason |> String.replace("\\", "\\\\") |> String.replace("\"", "\\\"")
" @deprecated(reason: \"#{escaped}\")"
" @deprecated(reason: \"#{escape_string_value(reason)}\")"
end

defp render_deprecated(%{"isDeprecated" => true}) do
Expand All @@ -419,6 +418,28 @@ defmodule GraphqlQuery.Schema.Remote.Introspection do

defp render_deprecated(_), do: ""

# escape StringValue following spec at
# https://spec.graphql.org/September2025/#sec-String)
defp escape_string_value(reason) do
reason
|> String.to_charlist()
|> Enum.map_join(&escape_codepoint/1)
end

defp escape_codepoint(?\\), do: "\\\\"
defp escape_codepoint(?"), do: "\\\""
defp escape_codepoint(?\b), do: "\\b"
defp escape_codepoint(?\f), do: "\\f"
defp escape_codepoint(?\n), do: "\\n"
defp escape_codepoint(?\r), do: "\\r"
defp escape_codepoint(?\t), do: "\\t"

defp escape_codepoint(c) when c < 0x20 or (c >= 0x7F and c <= 0x9F) do
"\\u" <> (c |> Integer.to_string(16) |> String.pad_leading(4, "0"))
end

defp escape_codepoint(c), do: <<c::utf8>>

# Default values — stored as JSON-encoded strings in introspection
defp render_default_value(nil), do: ""
defp render_default_value(value), do: " = #{value}"
Expand Down
75 changes: 75 additions & 0 deletions test/graphql_query/schema/remote/introspection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,81 @@ defmodule GraphqlQuery.Schema.Remote.IntrospectionTest do
assert sdl =~ "directive @deprecated"
end

test "escapes control characters in deprecation reasons so the SDL parses" do
schema = %{
"queryType" => %{"name" => "Query"},
"mutationType" => nil,
"subscriptionType" => nil,
"directives" => [],
"types" => [
%{
"kind" => "OBJECT",
"name" => "Query",
"description" => nil,
"interfaces" => [],
"fields" => [
%{
"name" => "old",
"description" => nil,
"args" => [],
"type" => %{"kind" => "SCALAR", "name" => "String", "ofType" => nil},
"isDeprecated" => true,
"deprecationReason" => "Use `new` instead.\nSee the docs for details.\n"
}
],
"inputFields" => nil,
"enumValues" => nil,
"possibleTypes" => nil
}
]
}

{:ok, sdl} = Introspection.to_sdl(schema)
assert sdl =~ ~S|@deprecated(reason: "Use `new` instead.\nSee the docs for details.\n")|
assert {:ok, _} = GraphqlQuery.Native.validate_schema(sdl, "test.graphql")
end

test "escapes the full set of GraphQL StringValue escapes in deprecation reasons" do
reason =
"quote:\" backslash:\\ bs:\b ff:\f lf:\n cr:\r tab:\t soh:\x01 nel:\u0085 unicode:café"

schema = %{
"queryType" => %{"name" => "Query"},
"mutationType" => nil,
"subscriptionType" => nil,
"directives" => [],
"types" => [
%{
"kind" => "OBJECT",
"name" => "Query",
"description" => nil,
"interfaces" => [],
"fields" => [
%{
"name" => "old",
"description" => nil,
"args" => [],
"type" => %{"kind" => "SCALAR", "name" => "String", "ofType" => nil},
"isDeprecated" => true,
"deprecationReason" => reason
}
],
"inputFields" => nil,
"enumValues" => nil,
"possibleTypes" => nil
}
]
}

{:ok, sdl} = Introspection.to_sdl(schema)

expected =
"@deprecated(reason: \"quote:\\\" backslash:\\\\ bs:\\b ff:\\f lf:\\n cr:\\r tab:\\t soh:\\u0001 nel:\\u0085 unicode:café\")"

assert sdl =~ expected
assert {:ok, _} = GraphqlQuery.Native.validate_schema(sdl, "test.graphql")
end

test "does not emit schema definition for standard root type names" do
json = load_fixture()
{:ok, sdl} = Introspection.to_sdl(json)
Expand Down
Loading