Skip to content
Open
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
12 changes: 6 additions & 6 deletions Cargo.lock

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

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ verbose_file_reads = "deny"
module_name_repetitions = "allow"

[workspace.dependencies]
gts = { version = "0.16.2", path = "gts" }
gts-cli = { version = "0.16.2", path = "gts-cli" }
gts-id = { version = "0.16.2", path = "gts-id" }
gts-macros = { version = "0.16.2", path = "gts-macros" }
gts-macros-cli = { version = "0.16.2", path = "gts-macros-cli" }
gts-validator = { version = "0.16.2", path = "gts-validator" }
gts = { version = "0.16.3", path = "gts" }
gts-cli = { version = "0.16.3", path = "gts-cli" }
gts-id = { version = "0.16.3", path = "gts-id" }
gts-macros = { version = "0.16.3", path = "gts-macros" }
gts-macros-cli = { version = "0.16.3", path = "gts-macros-cli" }
gts-validator = { version = "0.16.3", path = "gts-validator" }

# Core dependencies
serde = { version = "1.0", features = ["derive"] }
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Other GTS spec [Reference Implementation](https://github.com/globaltypesystem/gt
- [x] **x-gts-type-ref / x-gts-instance-ref** - kind-specific GTS entity references in schemas (spec §9.6.1)
- [x] **x-gts-pattern-ref** - pattern-valued string fields: GTS identifier pattern syntax and coverage by an operand, independent of the registry (spec §9.6.2, §10)
- [x] **YAML support** - to support YAML files (*.yml, *.yaml) as input files
- [x] **JSONC support** - to support JSONC files (*.jsonc) as input files
- [ ] **TypeSpec support** - add [typespec.io](https://typespec.io/) files (*.tsp) support
- [x] **UUID for instances** - to support UUID as ID in JSON instances (anonymous instances)

Expand Down Expand Up @@ -164,7 +165,7 @@ gts validate-id --gts-id "invalid-id"
Extract GTS identifiers from JSON objects. This happens automatically when loading files.

```bash
# List all entities (extracts IDs from all JSON/YAML files)
# List all entities (extracts IDs from all JSON/JSONC/YAML files)
gts --path ../gts-spec/examples list --limit 10
```

Expand Down Expand Up @@ -919,6 +920,11 @@ curl -X POST http://localhost:8000/entities \
-H "Content-Type: application/json" \
-d '{"gtsId": "gts.x.core.events.event.v1.0", "data": "..."}'

# Add entity from a JSONC body (comments and trailing commas allowed)
curl -X POST http://localhost:8000/entities \
-H "Content-Type: application/jsonc" \
--data-binary @event.jsonc

# Validate type schema (OP#12 - schema-vs-schema chain validation)
curl -X POST http://localhost:8000/validate-type-schema \
-H "Content-Type: application/json" \
Expand All @@ -927,7 +933,7 @@ curl -X POST http://localhost:8000/validate-type-schema \

## Configuration

Create a `gts.config.json` file to customize entity ID field detection:
Create a `gts.config.json` (or `gts.config.jsonc`, which may contain comments) file to customize entity ID field detection:

```json
{
Expand Down
2 changes: 1 addition & 1 deletion gts-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gts-cli"
version = "0.16.2"
version = "0.16.3"
edition.workspace = true
authors.workspace = true
license.workspace = true
Expand Down
6 changes: 4 additions & 2 deletions gts-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ pub enum Commands {
#[arg(long, default_value = "major")]
scope: String,
},
/// Validate all JSON documents in a file or directory
/// Validate all JSON/JSONC documents in a file or directory
ValidateJson {
/// JSON file or directory to scan
/// JSON/JSONC file or directory to scan
#[arg(long)]
path: Option<String>,
},
Expand Down Expand Up @@ -168,7 +168,9 @@ pub async fn run_with_cli(cli: Cli) -> Result<()> {
};

// Only initialize logging if not already initialized (for testing)
// Logs go to stderr so they never corrupt the JSON results on stdout.
let _ = tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.with_max_level(log_level)
.with_target(false)
.try_init();
Expand Down
80 changes: 69 additions & 11 deletions gts-cli/src/json_validation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use gts::entities::{GtsConfig, GtsEntity, GtsFile};
use gts::gts::{GTS_ID_PREFIX, GtsId};
use gts::jsonc;
use gts::store::GtsStore;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -84,13 +85,10 @@ impl GtsJsonValidator {
.unwrap_or_else(|_| self.path.clone());

if resolved.is_file() {
if resolved
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("json"))
{
if Self::is_json_family(&resolved) {
return vec![resolved];
}
self.add_issue_path(&resolved, "discovery", "Expected a .json file");
self.add_issue_path(&resolved, "discovery", "Expected a .json or .jsonc file");
return vec![];
}

Expand Down Expand Up @@ -120,11 +118,7 @@ impl GtsJsonValidator {
continue;
}

if path.is_file()
&& path
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("json"))
{
if path.is_file() && Self::is_json_family(path) {
let rp = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
let key = rp.to_string_lossy().to_string();
if seen.insert(key) {
Expand All @@ -137,6 +131,13 @@ impl GtsJsonValidator {
files
}

fn is_json_family(path: &Path) -> bool {
jsonc::is_jsonc_path(path)
|| path
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("json"))
}

fn read_file(&mut self, file_path: &Path) {
self.files += 1;
let content_str = match std::fs::read_to_string(file_path) {
Expand All @@ -147,7 +148,7 @@ impl GtsJsonValidator {
}
};

let content: Value = match serde_json::from_str(&content_str) {
let content: Value = match jsonc::from_str_for_path(file_path, &content_str) {
Ok(v) => v,
Err(e) => {
self.add_issue_path(file_path, "json", &e.to_string());
Expand Down Expand Up @@ -395,3 +396,60 @@ impl GtsJsonValidator {
});
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;

const SCHEMA_JSONC: &str = r#"// Thing type
{
"$id": "gts://gts.x.test.jsonc.thing.v1~", // type id
"$schema": "http://json-schema.org/draft-07/schema#",
/* block comment */
"type": "object",
"properties": {
"id": { "type": "string" },
},
}
"#;

fn validate(dir: &TempDir) -> GtsJsonValidationResult {
GtsJsonValidator::new(&dir.path().to_string_lossy(), GtsConfig::default()).validate()
}

#[test]
fn validates_jsonc_documents() {
let dir = TempDir::new().unwrap();
fs::write(dir.path().join("thing.schema.jsonc"), SCHEMA_JSONC).unwrap();
fs::write(
dir.path().join("one.jsonc"),
"{\n // instance\n \"id\": \"gts.x.test.jsonc.thing.v1~x.test.jsonc.one.v1\",\n}\n",
)
.unwrap();

let result = validate(&dir);
assert!(result.ok, "{:?}", result.issues);
assert_eq!((result.files, result.schemas, result.instances), (2, 1, 1));

let single = GtsJsonValidator::new(
&dir.path().join("thing.schema.jsonc").to_string_lossy(),
GtsConfig::default(),
)
.validate();
assert!(single.ok, "{:?}", single.issues);
assert_eq!(single.schemas, 1);
}

#[test]
fn comments_in_json_files_are_reported() {
let dir = TempDir::new().unwrap();
fs::write(dir.path().join("thing.schema.json"), SCHEMA_JSONC).unwrap();

let result = validate(&dir);
assert!(!result.ok);
assert_eq!(result.issues.len(), 1);
assert_eq!(result.issues[0].stage, "json");
}
}
52 changes: 47 additions & 5 deletions gts-cli/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use axum::{
Json, Router,
extract::{Path, Query, State},
http::StatusCode,
middleware,
response::IntoResponse,
body::{Body, Bytes},
extract::{FromRequest, Path, Query, Request, State},
http::{HeaderMap, HeaderValue, StatusCode, header},
middleware::{self, Next},
response::{IntoResponse, Response},
routing::{get, post},
};
use gts::GtsOps;
use gts::GtsRefValidation;
use gts::jsonc::{self, JSONC_MEDIA_TYPE};
use gts::ops::AddEntityRejection;
use serde::Deserialize;
use serde_json::{Value, json};
use std::borrow::Cow;
use std::sync::{Arc, Mutex};

use crate::logging::LoggingMiddleware;
Expand Down Expand Up @@ -83,7 +86,8 @@ impl GtsHttpServer {
.route("/cast", post(cast))
.route("/query", get(query))
.route("/attr", get(attr))
.with_state(state);
.with_state(state)
.layer(middleware::from_fn(accept_jsonc_body));

// Add custom logging middleware if verbose >= 1
if verbose >= 1 {
Expand Down Expand Up @@ -181,6 +185,44 @@ impl GtsRefValidationQuery {
}
}

fn is_jsonc_request(headers: &HeaderMap) -> bool {
headers
.get(header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.split(';').next())
.is_some_and(|mime| mime.trim().eq_ignore_ascii_case(JSONC_MEDIA_TYPE))
}

/// Accept `application/jsonc` bodies on every endpoint: comments and trailing
/// commas are blanked out and the request is passed on as `application/json`,
/// so the `Json` extractors and their rejections apply unchanged.
///
/// The body is buffered with axum's `Bytes` extractor, so the effective
/// `DefaultBodyLimit` (axum's default unless a `DefaultBodyLimit` layer is
/// applied outside this middleware) and its 413/400 rejections are the same as
/// for `application/json`.
async fn accept_jsonc_body(request: Request, next: Next) -> Response {
if !is_jsonc_request(request.headers()) {
return next.run(request).await;
}
let (mut parts, body) = request.into_parts();
let bytes = match Bytes::from_request(Request::from_parts(parts.clone(), body), &()).await {
Ok(bytes) => bytes,
Err(rejection) => return rejection.into_response(),
};
// Stripping preserves the length, so `Content-Length` stays valid.
let stripped = match std::str::from_utf8(&bytes).map(jsonc::strip) {
Ok(Cow::Owned(text)) => Some(text),
_ => None,
};
let body = stripped.map_or_else(|| Body::from(bytes), Body::from);
parts.headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/json"),
);
next.run(Request::from_parts(parts, body)).await
}

fn unprocessable(error: &str) -> axum::response::Response {
(
StatusCode::UNPROCESSABLE_ENTITY,
Expand Down
Loading
Loading