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

Add a --without-id/-w option to bundle without identifiers #90

Merged
merged 1 commit into from
Jun 17, 2024
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
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vendorpull https://github.com/sourcemeta/vendorpull dea311b5bfb53b6926a4140267959ae334d3ecf4
noa https://github.com/sourcemeta/noa 2bc3138b80e575786bec418c91fc2058c6836993
jsontoolkit https://github.com/sourcemeta/jsontoolkit e8eddd077e4d7af996fd892a19ef8ad244ff3d59
jsontoolkit https://github.com/sourcemeta/jsontoolkit 0e2ac8987382685ad6dc50ca33d2e43a6701b023
hydra https://github.com/sourcemeta/hydra 3c53d3fdef79e9ba603d48470a508cc45472a0dc
21 changes: 20 additions & 1 deletion docs/bundle.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Bundling
```sh
jsonschema bundle <schema.json>
[--http/-h] [--verbose/-v] [--resolve/-r <schemas-or-directories> ...]
[--ignore/-i <schemas-or-directories>]
[--extension/-e <extension>] [--ignore/-i <schemas-or-directories>]
[--without-id/-w]
```

A schema may contain references to remote schemas outside the scope of the
Expand All @@ -16,6 +17,18 @@ to resolve remote references in advance and inline them into the given schema
for local consumption or further distribution. The JSON Schema CLI supports
this functionality through the `bundle` command.

> [!WARNING]
> A popular use case for JSON Schema is providing auto-completion for code
> editors. If you plan to use your bundled schema for this, keep in mind that
> at the time of this writing, Visual Studio Code ships with a
> [non-compliant](https://bowtie.report/#/implementations/ts-vscode-json-languageservice)
> implementation that [does not support the `$id` and `id`
> keywords](https://github.com/microsoft/vscode-json-languageservice/issues/224)
> and that will fail to handle most bundled schemas. As a workaround, we offer
> the `--without-id`/`-w` option to perform bundling without relying on
> identifiers to make your resulting schemas compatible with Visual Studio
> Code.

Examples
--------

Expand Down Expand Up @@ -97,3 +110,9 @@ jsonschema bundle path/to/my/schema.json \
```sh
jsonschema bundle path/to/my/schema.json --http
```

### Bundle a JSON Schema without identifiers and importing a single local schema

```sh
jsonschema bundle path/to/my/schema.json --resolve path/to/external.json --without-id
```
23 changes: 18 additions & 5 deletions src/command_bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@

auto intelligence::jsonschema::cli::bundle(
const std::span<const std::string> &arguments) -> int {
const auto options{parse_options(arguments, {"h", "http"})};
const auto options{
parse_options(arguments, {"h", "http", "w", "without-id"})};
CLI_ENSURE(!options.at("").empty(), "You must pass a JSON Schema as input");
auto schema{sourcemeta::jsontoolkit::from_file(options.at("").front())};
sourcemeta::jsontoolkit::bundle(
schema, sourcemeta::jsontoolkit::default_schema_walker,
resolver(options, options.contains("h") || options.contains("http")))
.wait();

if (options.contains("w") || options.contains("without-id")) {
log_verbose(options) << "Bundling without using identifiers\n";
sourcemeta::jsontoolkit::bundle(
schema, sourcemeta::jsontoolkit::default_schema_walker,
resolver(options, options.contains("h") || options.contains("http")),
sourcemeta::jsontoolkit::BundleOptions::WithoutIdentifiers)
.wait();
} else {
sourcemeta::jsontoolkit::bundle(
schema, sourcemeta::jsontoolkit::default_schema_walker,
resolver(options, options.contains("h") || options.contains("http")),
sourcemeta::jsontoolkit::BundleOptions::Default)
.wait();
}

sourcemeta::jsontoolkit::prettify(
schema, std::cout, sourcemeta::jsontoolkit::schema_format_compare);
std::cout << std::endl;
Expand Down
5 changes: 3 additions & 2 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ Global Options:
Format the input schemas in-place or check they are formatted.

lint [schemas-or-directories...] [--fix/-f] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>]
[--ignore/-i <schemas-or-directories>]

Lint the input schemas and potentially fix the reported issues.

bundle <schema.json> [--http/-h]
bundle <schema.json> [--http/-h] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>] [--without-id/-w]

Perform JSON Schema Bundling on a schema to inline remote references,
printing the result to standard output.
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ add_jsonschema_test_unix(bundle_non_remote)
add_jsonschema_test_unix(bundle_remote_single_schema)
add_jsonschema_test_unix(bundle_remote_no_http)
add_jsonschema_test_unix(bundle_remote_directory)
add_jsonschema_test_unix(bundle_remote_directory_without_id)
add_jsonschema_test_unix(test_single_pass)
add_jsonschema_test_unix(test_single_fail)
add_jsonschema_test_unix(test_single_unsupported)
Expand Down
42 changes: 42 additions & 0 deletions test/bundle_remote_directory_without_id.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com",
"$ref": "nested"
}
EOF

mkdir "$TMP/schemas"
cat << 'EOF' > "$TMP/schemas/remote.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/nested",
"type": "string"
}
EOF

"$1" bundle "$TMP/schema.json" --resolve "$TMP/schemas" --without-id > "$TMP/result.json"

cat << 'EOF' > "$TMP/expected.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "#/$defs/https%3A~1~1example.com~1nested",
"$defs": {
"https://example.com/nested": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string"
}
}
}
EOF

diff "$TMP/result.json" "$TMP/expected.json"
4 changes: 4 additions & 0 deletions vendor/jsontoolkit/src/jsonschema/bundle.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.