Skip to content

Commit 2b2953c

Browse files
committed
Use php-ext-yaml and remove symfony/yaml component usage
1 parent 02f7da3 commit 2b2953c

File tree

8 files changed

+76
-66
lines changed

8 files changed

+76
-66
lines changed

src/Reader.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use cebe\openapi\json\InvalidJsonPointerSyntaxException;
1414
use cebe\openapi\json\JsonPointer;
1515
use cebe\openapi\spec\OpenApi;
16-
use Symfony\Component\Yaml\Yaml;
1716

1817
/**
1918
* Utility class to simplify reading JSON or YAML OpenAPI specs.
@@ -54,7 +53,7 @@ public static function readFromJson(string $json, string $baseType = OpenApi::cl
5453
*/
5554
public static function readFromYaml(string $yaml, string $baseType = OpenApi::class): SpecObjectInterface
5655
{
57-
return new $baseType(Yaml::parse($yaml));
56+
return new $baseType(yaml_parse($yaml));
5857
}
5958

6059
/**

src/ReferenceContext.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use cebe\openapi\exceptions\UnresolvableReferenceException;
1212
use cebe\openapi\json\JsonPointer;
1313
use cebe\openapi\spec\Reference;
14-
use Symfony\Component\Yaml\Yaml;
1514

1615
/**
1716
* ReferenceContext represents a context in which references are resolved.
@@ -224,7 +223,7 @@ public function fetchReferencedFile($uri)
224223
if (strpos(ltrim($content), '{') === 0) {
225224
$parsedContent = json_decode($content, true);
226225
} else {
227-
$parsedContent = Yaml::parse($content);
226+
$parsedContent = yaml_parse($content);
228227
}
229228
$this->_cache->set('FILE_CONTENT://' . $uri, 'FILE_CONTENT', $parsedContent);
230229
return $parsedContent;

src/Writer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use cebe\openapi\exceptions\IOException;
1111
use cebe\openapi\spec\OpenApi;
12-
use Symfony\Component\Yaml\Yaml;
1312

1413
/**
1514
* Utility class to simplify writing JSON or YAML OpenAPI specs.
@@ -35,7 +34,11 @@ public static function writeToJson(SpecObjectInterface $object, int $flags = JSO
3534
*/
3635
public static function writeToYaml(SpecObjectInterface $object): string
3736
{
38-
return Yaml::dump($object->getSerializableData(), 256, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
37+
// return Yaml::dump($object->getSerializableData(), 256, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
38+
return yaml_emit(
39+
json_decode(json_encode($object->getSerializableData(), JSON_FORCE_OBJECT), true),
40+
YAML_UTF8_ENCODING
41+
);
3942
}
4043

4144
/**

src/spec/Reference.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace cebe\openapi\spec;
99

1010
use cebe\openapi\DocumentContextInterface;
11-
use cebe\openapi\exceptions\IOException;
1211
use cebe\openapi\exceptions\TypeErrorException;
1312
use cebe\openapi\exceptions\UnresolvableReferenceException;
1413
use cebe\openapi\json\InvalidJsonPointerSyntaxException;
@@ -17,7 +16,6 @@
1716
use cebe\openapi\json\NonexistentJsonPointerReferenceException;
1817
use cebe\openapi\ReferenceContext;
1918
use cebe\openapi\SpecObjectInterface;
20-
use Symfony\Component\Yaml\Yaml;
2119

2220
/**
2321
* Reference Object

tests/WriterTest.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ public function testWriteYaml()
7474

7575

7676
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
77+
---
7778
openapi: 3.0.0
7879
info:
79-
title: 'Test API'
80+
title: Test API
8081
version: 1.0.0
81-
paths: { }
82+
paths: []
83+
...
8284
8385
YAML
8486
),
@@ -121,12 +123,14 @@ public function testWriteEmptySecurityYaml()
121123

122124

123125
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
126+
---
124127
openapi: 3.0.0
125128
info:
126-
title: 'Test API'
129+
title: Test API
127130
version: 1.0.0
128-
paths: { }
131+
paths: []
129132
security: []
133+
...
130134
131135
YAML
132136
),
@@ -173,14 +177,15 @@ public function testWriteEmptySecurityPartYaml()
173177

174178

175179
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
180+
---
176181
openapi: 3.0.0
177182
info:
178-
title: 'Test API'
183+
title: Test API
179184
version: 1.0.0
180-
paths: { }
185+
paths: []
181186
security:
182-
-
183-
Bearer: []
187+
- Bearer: []
188+
...
184189
185190
YAML
186191
),

tests/spec/MediaTypeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
use cebe\openapi\Reader;
4-
use cebe\openapi\spec\MediaType;
54
use cebe\openapi\spec\Example;
5+
use cebe\openapi\spec\MediaType;
66
use cebe\openapi\spec\OpenApi;
77
use cebe\openapi\spec\Reference;
88
use Symfony\Component\Yaml\Yaml;
@@ -125,7 +125,7 @@ public function testPathsCanNotBeCreatedFromBullshit($config, $expectedException
125125

126126
public function testUnresolvedReferencesInEncoding()
127127
{
128-
$yaml = Yaml::parse(<<<'YAML'
128+
$yaml = yaml_parse(<<<'YAML'
129129
openapi: "3.0.0"
130130
info:
131131
version: 1.0.0

tests/spec/OpenApiTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testReadPetStore()
3030
{
3131
$openApiFile = __DIR__ . '/../../vendor/oai/openapi-specification/examples/v3.0/petstore.yaml';
3232

33-
$yaml = Yaml::parse(file_get_contents($openApiFile));
33+
$yaml = yaml_parse(file_get_contents($openApiFile));
3434
$openapi = new OpenApi($yaml);
3535

3636
$result = $openapi->validate();

tests/spec/ReferenceTest.php

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22

33
use cebe\openapi\Reader;
4+
use cebe\openapi\spec\Example;
45
use cebe\openapi\spec\OpenApi;
56
use cebe\openapi\spec\Parameter;
67
use cebe\openapi\spec\Reference;
78
use cebe\openapi\spec\RequestBody;
89
use cebe\openapi\spec\Response;
910
use cebe\openapi\spec\Schema;
10-
use cebe\openapi\spec\Example;
1111

1212
/**
1313
* @covers \cebe\openapi\spec\Reference
@@ -441,21 +441,23 @@ public function testTransitiveReferenceOverTwoFiles()
441441
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
442442

443443
$expected = <<<YAML
444+
---
444445
openapi: 3.0.0
445446
info:
446-
title: 'Ref Example'
447+
title: Ref Example
447448
version: 1.0.0
448449
paths:
449450
/pet:
450451
get:
451452
responses:
452-
'200':
453-
description: 'return a pet'
453+
200:
454+
description: return a pet
454455
/cat:
455456
get:
456457
responses:
457-
'200':
458-
description: 'return a cat'
458+
200:
459+
description: return a cat
460+
...
459461
460462
YAML;
461463
// remove line endings to make string equal on windows
@@ -474,53 +476,53 @@ public function testReferencedCommonParamsInReferencedPath()
474476
$openapi = Reader::readFromYamlFile(__DIR__ . '/data/reference/ReferencedCommonParamsInReferencedPath.yml', OpenApi::class, \cebe\openapi\ReferenceContext::RESOLVE_MODE_INLINE);
475477
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
476478
$expected = <<<YAML
479+
---
477480
openapi: 3.0.0
478481
info:
479-
title: 'Nested reference with common path params'
482+
title: Nested reference with common path params
480483
version: 1.0.0
481484
paths:
482485
/example:
483486
get:
484487
responses:
485-
'200':
486-
description: 'OK if common params can be references'
488+
200:
489+
description: OK if common params can be references
487490
request:
488491
content:
489492
application/json:
490493
examples:
491494
user:
492-
summary: 'User Example'
495+
summary: User Example
493496
externalValue: ./paths/examples/user-example.json
494497
userex:
495-
summary: 'External User Example'
496-
externalValue: 'https://api.example.com/examples/user-example.json'
498+
summary: External User Example
499+
externalValue: https://api.example.com/examples/user-example.json
497500
parameters:
498-
-
499-
name: test
500-
in: header
501-
description: 'Test parameter to be referenced'
502-
required: true
503-
schema:
504-
enum:
505-
- test
506-
type: string
501+
- name: test
502+
in: header
503+
description: Test parameter to be referenced
504+
required: true
505+
schema:
506+
enum:
507+
- test
508+
type: string
507509
x-something: something
508510
/something:
509511
get:
510512
responses:
511-
'200':
512-
description: 'OK if common params can be references'
513+
200:
514+
description: OK if common params can be references
513515
parameters:
514-
-
515-
name: test
516-
in: header
517-
description: 'Test parameter to be referenced'
518-
required: true
519-
schema:
520-
enum:
521-
- test
522-
type: string
516+
- name: test
517+
in: header
518+
description: Test parameter to be referenced
519+
required: true
520+
schema:
521+
enum:
522+
- test
523+
type: string
523524
x-something: something
525+
...
524526
525527
YAML;
526528
// remove line endings to make string equal on windows
@@ -541,16 +543,17 @@ public function testResolveRelativePathInline()
541543
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
542544

543545
$expected = <<<YAML
546+
---
544547
openapi: 3.0.3
545548
info:
546-
title: 'Link Example'
549+
title: Link Example
547550
version: 1.0.0
548551
paths:
549552
/pet:
550553
get:
551554
responses:
552-
'200':
553-
description: 'return a pet'
555+
200:
556+
description: return a pet
554557
components:
555558
schemas:
556559
Pet:
@@ -561,7 +564,7 @@ public function testResolveRelativePathInline()
561564
format: int64
562565
cat:
563566
\$ref: '#/components/schemas/Cat'
564-
description: 'A Pet'
567+
description: A Pet
565568
Cat:
566569
type: object
567570
properties:
@@ -570,10 +573,11 @@ public function testResolveRelativePathInline()
570573
format: int64
571574
name:
572575
type: string
573-
description: 'the cats name'
576+
description: the cats name
574577
pet:
575578
\$ref: '#/components/schemas/Pet'
576-
description: 'A Cat'
579+
description: A Cat
580+
...
577581
578582
YAML;
579583
// remove line endings to make string equal on windows
@@ -594,16 +598,17 @@ public function testResolveRelativePathAll()
594598
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
595599

596600
$expected = <<<YAML
601+
---
597602
openapi: 3.0.3
598603
info:
599-
title: 'Link Example'
604+
title: Link Example
600605
version: 1.0.0
601606
paths:
602607
/pet:
603608
get:
604609
responses:
605-
'200':
606-
description: 'return a pet'
610+
200:
611+
description: return a pet
607612
components:
608613
schemas:
609614
Pet:
@@ -620,11 +625,11 @@ public function testResolveRelativePathAll()
620625
format: int64
621626
name:
622627
type: string
623-
description: 'the cats name'
628+
description: the cats name
624629
pet:
625630
\$ref: '#/components/schemas/Pet'
626-
description: 'A Cat'
627-
description: 'A Pet'
631+
description: A Cat
632+
description: A Pet
628633
Cat:
629634
type: object
630635
properties:
@@ -633,7 +638,7 @@ public function testResolveRelativePathAll()
633638
format: int64
634639
name:
635640
type: string
636-
description: 'the cats name'
641+
description: the cats name
637642
pet:
638643
type: object
639644
properties:
@@ -642,8 +647,9 @@ public function testResolveRelativePathAll()
642647
format: int64
643648
cat:
644649
\$ref: '#/components/schemas/Cat'
645-
description: 'A Pet'
646-
description: 'A Cat'
650+
description: A Pet
651+
description: A Cat
652+
...
647653
648654
YAML;
649655
// remove line endings to make string equal on windows

0 commit comments

Comments
 (0)