Skip to content

Commit c919d86

Browse files
use YAML::XS for speed
thank you tinita! ingydotnet/yaml-libyaml-pm#110
1 parent 8513a09 commit c919d86

File tree

8 files changed

+67
-63
lines changed

8 files changed

+67
-63
lines changed

examples/synopsis

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ use 5.020;
55
use OpenAPI::Modern;
66
use HTTP::Request::Common;
77
use Mojo::Message::Response;
8-
use YAML::PP;
8+
use YAML::XS 0.87;
9+
10+
local $YAML::XS::Boolean = 'JSON::PP';
911

1012
my $openapi = OpenAPI::Modern->new(
1113
openapi_uri => '/api',
12-
openapi_schema => YAML::PP->new(boolean => 'JSON::PP')->load_string(<<'YAML'));
14+
openapi_schema => Load(<<'YAML'));
1315
openapi: 3.1.0
1416
info:
1517
title: Test API

lib/OpenAPI/Modern.pm

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,13 @@ __END__
690690
691691
=head1 SYNOPSIS
692692
693+
use YAML::XS 0.87;
694+
use OpenAPI::Modern;
695+
local $YAML::XS::Boolean = 'JSON::PP';
696+
693697
my $openapi = OpenAPI::Modern->new(
694698
openapi_uri => '/api',
695-
openapi_schema => YAML::PP->new(boolean => 'JSON::PP')->load_string(<<'YAML'));
699+
openapi_schema => Load(<<'YAML'));
696700
openapi: 3.1.0
697701
info:
698702
title: Test API

t/find_path.t

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use OpenAPI::Modern;
1616
use JSON::Schema::Modern::Utilities 'jsonp';
1717
use Test::File::ShareDir -share => { -dist => { 'OpenAPI-Modern' => 'share' } };
1818
use constant { true => JSON::PP::true, false => JSON::PP::false };
19-
use YAML::PP 0.005;
2019

2120
use lib 't/lib';
2221
use Helper;
@@ -32,12 +31,11 @@ YAML
3231
# the absolute uri we will see in errors
3332
my $doc_uri_rel = Mojo::URL->new('/api');
3433
my $doc_uri = $doc_uri_rel->to_abs(Mojo::URL->new('https://example.com'));
35-
my $yamlpp = YAML::PP->new(boolean => 'JSON::PP');
3634

3735
subtest 'bad conversion to Mojo::Message::Request' => sub {
3836
my $openapi = OpenAPI::Modern->new(
3937
openapi_uri => '/api',
40-
openapi_schema => $yamlpp->load_string(<<YAML));
38+
openapi_schema => yaml(<<YAML));
4139
$openapi_preamble
4240
paths: {}
4341
YAML
@@ -71,7 +69,7 @@ note 'REQUEST/RESPONSE TYPE: '.$::TYPE;
7169
subtest 'request is parsed to get path information' => sub {
7270
my $openapi = OpenAPI::Modern->new(
7371
openapi_uri => '/api',
74-
openapi_schema => $yamlpp->load_string(<<YAML));
72+
openapi_schema =>yaml(<<YAML));
7573
$openapi_preamble
7674
paths:
7775
/foo/{foo_id}:
@@ -406,7 +404,7 @@ YAML
406404

407405
$openapi = OpenAPI::Modern->new(
408406
openapi_uri => '/api',
409-
openapi_schema => $yamlpp->load_string(<<YAML));
407+
openapi_schema => yaml(<<YAML));
410408
$openapi_preamble
411409
paths:
412410
/foo: {}
@@ -437,7 +435,7 @@ YAML
437435

438436
$openapi = OpenAPI::Modern->new(
439437
openapi_uri => '/api',
440-
openapi_schema => $yamlpp->load_string(<<YAML));
438+
openapi_schema => yaml(<<YAML));
441439
$openapi_preamble
442440
paths:
443441
/foo/{foo_id}:
@@ -580,7 +578,7 @@ YAML
580578

581579
$openapi = OpenAPI::Modern->new(
582580
openapi_uri => '/api',
583-
openapi_schema => $yamlpp->load_string(<<YAML));
581+
openapi_schema => yaml(<<YAML));
584582
$openapi_preamble
585583
paths:
586584
/foo/{foo_id}/bar/{foo_id}:
@@ -612,7 +610,7 @@ YAML
612610

613611
$openapi = OpenAPI::Modern->new(
614612
openapi_uri => '/api',
615-
openapi_schema => $yamlpp->load_string(<<YAML));
613+
openapi_schema => yaml(<<YAML));
616614
$openapi_preamble
617615
paths:
618616
/foo/{foo_id}:
@@ -655,7 +653,7 @@ YAML
655653
subtest 'no request is provided: options are relied on as the sole source of truth' => sub {
656654
my $openapi = OpenAPI::Modern->new(
657655
openapi_uri => '/api',
658-
openapi_schema => $yamlpp->load_string(<<YAML));
656+
openapi_schema => yaml(<<YAML));
659657
$openapi_preamble
660658
paths:
661659
/foo/{foo_id}:
@@ -747,7 +745,7 @@ YAML
747745

748746
$openapi = OpenAPI::Modern->new(
749747
openapi_uri => '/api',
750-
openapi_schema => $yamlpp->load_string(<<YAML));
748+
openapi_schema => yaml(<<YAML));
751749
$openapi_preamble
752750
paths:
753751
/foo/{foo_id}:

t/lib/Helper.pm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use HTTP::Response;
1313
use HTTP::Status ();
1414
use Mojo::Message::Request;
1515
use Mojo::Message::Response;
16+
use YAML::XS 0.87;
1617

1718
# type can be
1819
# 'lwp': classes of type URI, HTTP::Headers, HTTP::Request, HTTP::Response
@@ -98,4 +99,9 @@ sub document_result ($document) {
9899
);
99100
}
100101

102+
sub yaml ($string) {
103+
local $YAML::XS::Boolean = 'JSON::PP';
104+
Load(Encode::encode('UTF-8', $string));
105+
}
106+
101107
1;

t/operationIds.t

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ use Test::Deep;
1515
use JSON::Schema::Modern;
1616
use JSON::Schema::Modern::Document::OpenAPI;
1717
use JSON::Schema::Modern::Utilities 'jsonp';
18-
use YAML::PP;
1918
use Test::File::ShareDir -share => { -dist => { 'OpenAPI-Modern' => 'share' } };
2019

2120
# the document where most constraints are defined
2221
use constant SCHEMA => 'https://spec.openapis.org/oas/3.1/schema/2022-10-07';
2322

24-
my $yamlpp = YAML::PP->new(boolean => 'JSON::PP');
25-
2623
subtest 'extract operationIds and identify duplicates' => sub {
2724
my $yaml = <<'YAML';
2825
---
@@ -68,7 +65,7 @@ YAML
6865
my $doc = JSON::Schema::Modern::Document::OpenAPI->new(
6966
canonical_uri => 'http://localhost:1234/api',
7067
evaluator => my $js = JSON::Schema::Modern->new,
71-
schema => $yamlpp->load_string($yaml),
68+
schema => yaml($yaml),
7269
);
7370

7471
ok(!$doc->errors, 'no errors when parsing this document');
@@ -90,7 +87,7 @@ YAML
9087
$doc = JSON::Schema::Modern::Document::OpenAPI->new(
9188
canonical_uri => 'http://localhost:1234/api',
9289
evaluator => $js = JSON::Schema::Modern->new,
93-
schema => $yamlpp->load_string($yaml =~ s/operation_id_[a-z]/operation_id_dupe/gr),
90+
schema => yaml($yaml =~ s/operation_id_[a-z]/operation_id_dupe/gr),
9491
);
9592

9693
cmp_deeply(

0 commit comments

Comments
 (0)