Skip to content

Commit 54628b5

Browse files
process effective_base_uri a little more efficiently
...and enforce that it has no fragment
1 parent 3abfd1f commit 54628b5

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Revision history for JSON-Schema-Modern
22

33
{{$NEXT}}
44
- fix Sereal serialization hooks
5+
- various performance improvements when dealing with base URIs
56

67
0.602 2025-02-21 00:11:32Z
78
- more checks at traverse and evaluation time for inconsistent

lib/JSON/Schema/Modern.pm

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ sub traverse ($self, $schema_reference, $config_override = {}) {
300300
initial_schema_uri => $initial_uri, # the canonical URI as of the start of this method or last $id
301301
traversed_schema_path => $initial_path, # the accumulated traversal path as of the start or last $id
302302
schema_path => '', # the rest of the path, since the start of this method or last $id
303-
effective_base_uri => Mojo::URL->new(''),
304303
errors => [],
305304
identifiers => {},
306305
subschemas => [],
@@ -348,25 +347,27 @@ sub traverse ($self, $schema_reference, $config_override = {}) {
348347
sub evaluate ($self, $data, $schema_reference, $config_override = {}) {
349348
croak 'evaluate called in void context' if not defined wantarray;
350349

351-
my $initial_path = $config_override->{traversed_schema_path} // '';
352-
my $effective_base_uri = Mojo::URL->new($config_override->{effective_base_uri}//'');
350+
my %overrides = %$config_override;
351+
delete @overrides{qw(validate_formats validate_content_schemas short_circuit collect_annotations scalarref_booleans stringy_numbers strict callbacks effective_base_uri data_path traversed_schema_path _strict_schema_data)};
352+
croak join(', ', sort keys %overrides), ' not supported as a config override in evaluate'
353+
if keys %overrides;
353354

354355
my $state = {
355356
data_path => $config_override->{data_path} // '',
356-
traversed_schema_path => $initial_path, # the accumulated path as of the start of evaluation or last $id or $ref
357+
traversed_schema_path => $config_override->{traversed_schema_path} // '', # the accumulated path as of the start of evaluation or last $id or $ref
357358
initial_schema_uri => Mojo::URL->new, # the canonical URI as of the start of evaluation or last $id or $ref
358359
schema_path => '', # the rest of the path, since the start of evaluation or last $id or $ref
359-
effective_base_uri => $effective_base_uri, # resolve locations against this for errors and annotations
360360
errors => [],
361361
depth => 0,
362362
configs => {},
363363
};
364364

365-
# note this is not quite the same list as what we use when defining $state below
366-
my %overrides = %$config_override;
367-
delete @overrides{qw(validate_formats validate_content_schemas short_circuit collect_annotations scalarref_booleans stringy_numbers strict callbacks effective_base_uri data_path traversed_schema_path _strict_schema_data)};
368-
croak join(', ', sort keys %overrides), ' not supported as a config override in evaluate'
369-
if keys %overrides;
365+
# resolve locations against this for errors and annotations, if locations are not already absolute
366+
if (length $config_override->{effective_base_uri}) {
367+
$state->{effective_base_uri} = Mojo::URL->new($config_override->{effective_base_uri});
368+
croak 'it is meaningless for effective_base_uri to have a fragment'
369+
if defined $state->{effective_base_uri}->fragment;
370+
}
370371

371372
my $valid;
372373
try {

lib/JSON/Schema/Modern/ResultNode.pm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ has absolute_keyword_location => (
4444
my $effective_base_uri = pop @$uri_bits;
4545
my ($initial_schema_uri, $schema_path, @extra_path) = @$uri_bits;
4646

47-
return $initial_schema_uri if not @extra_path and not length($schema_path);
47+
return($initial_schema_uri eq '' && $self->{keyword_location} eq '' ? undef : $initial_schema_uri)
48+
if not @extra_path and not length($schema_path) and not length $effective_base_uri;
49+
4850
my $uri = $initial_schema_uri->clone;
4951
my $fragment = ($uri->fragment//'').(@extra_path ? jsonp($schema_path, @extra_path) : $schema_path);
5052
undef $fragment if not length($fragment);
5153
$uri->fragment($fragment);
5254

53-
$uri = $uri->to_abs($effective_base_uri);
55+
$uri = $uri->to_abs($effective_base_uri) if length $effective_base_uri;
5456

5557
undef $uri if $uri eq '' and $self->{keyword_location} eq ''
5658
or ($uri->fragment // '') eq $self->{keyword_location} and $uri->clone->fragment(undef) eq '';

lib/JSON/Schema/Modern/Utilities.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ sub E ($state, $error_string, @args) {
323323

324324
# we store the absolute uri in unresolved form until needed,
325325
# and perform the rest of the calculations later.
326-
my $uri = [ $state->{initial_schema_uri}, $state->{schema_path}, $state->{keyword}, @schema_path_suffix, $state->{effective_base_uri} ];
326+
my $uri = [ $state->{initial_schema_uri}, $state->{schema_path}, ($state->{keyword}//()), @schema_path_suffix, $state->{effective_base_uri} ];
327327

328328
my $keyword_location = $state->{traversed_schema_path}
329329
.jsonp($state->{schema_path}, $state->{keyword}, @schema_path_suffix);

0 commit comments

Comments
 (0)