Skip to content

Commit

Permalink
Automatically serve without identifiers for VS Code (#116)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Jan 10, 2025
1 parent 9e01fdb commit b3179bf
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,16 @@ static auto on_request(const sourcemeta::hydra::http::ServerLogger &logger,

const auto schema_identifier{request_path_to_schema_uri(
configuration().at("url").to_string(), request_path)};
const auto maybe_schema{resolver(schema_identifier,
request.query("bundle").has_value(),
request.query("unidentify").has_value())};

// Because Visual Studio Code famously does not support `$id` or `id`
// See https://github.com/microsoft/vscode-json-languageservice/issues/224
const auto user_agent{request.header("user-agent").value_or("")};
const auto is_vscode{user_agent.starts_with("Visual Studio Code") ||
user_agent.starts_with("VSCodium")};

const auto maybe_schema{resolver(
schema_identifier, is_vscode || request.query("bundle").has_value(),
is_vscode || request.query("unidentify").has_value())};
if (!maybe_schema.has_value()) {
json_error(logger, request, response,
sourcemeta::hydra::http::Status::NOT_FOUND, "not-found",
Expand Down
79 changes: 79 additions & 0 deletions test/e2e/common/vscode.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
GET {{base}}/example/bundling/single.json
User-Agent: Visual Studio Code (desktop)
HTTP 200
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Bundling",
"description": "A bundling example",
"properties": {
"foo": {
"$ref": "#/definitions/http%3A~1~1localhost%3A8000~1example~1v2.0~1schema.json"
}
},
"definitions": {
"http://localhost:8000/example/v2.0/schema.json": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "integer"
}
}
}

GET {{base}}/example/bundling/single.json
User-Agent: VSCodium (desktop)
HTTP 200
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Bundling",
"description": "A bundling example",
"properties": {
"foo": {
"$ref": "#/definitions/http%3A~1~1localhost%3A8000~1example~1v2.0~1schema.json"
}
},
"definitions": {
"http://localhost:8000/example/v2.0/schema.json": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "integer"
}
}
}

GET {{base}}/example/bundling/single.json?bundle=0
User-Agent: VSCodium (desktop)
HTTP 200
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Bundling",
"description": "A bundling example",
"properties": {
"foo": {
"$ref": "#/definitions/http%3A~1~1localhost%3A8000~1example~1v2.0~1schema.json"
}
},
"definitions": {
"http://localhost:8000/example/v2.0/schema.json": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "integer"
}
}
}

GET {{base}}/example/bundling/single.json?unidentify=0
User-Agent: VSCodium (desktop)
HTTP 200
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Bundling",
"description": "A bundling example",
"properties": {
"foo": {
"$ref": "#/definitions/http%3A~1~1localhost%3A8000~1example~1v2.0~1schema.json"
}
},
"definitions": {
"http://localhost:8000/example/v2.0/schema.json": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "integer"
}
}
}

0 comments on commit b3179bf

Please sign in to comment.