Skip to content

Commit 8567bec

Browse files
committed
WIP
1 parent c0c6486 commit 8567bec

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"php": ">=7.1.0",
2222
"ext-json": "*",
2323
"symfony/yaml": "^3.4 || ^4 || ^5 || ^6 || ^7.0",
24-
"justinrainbow/json-schema": "^5.2 || ^6.0"
24+
"justinrainbow/json-schema": "^5.2 || ^6.0",
25+
"salsify/json-streaming-parser": "^8.3"
2526
},
2627
"require-dev": {
2728
"cebe/indent": "*",

src/Reader.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use cebe\openapi\json\JsonPointer;
1515
use cebe\openapi\spec\OpenApi;
1616
use Symfony\Component\Yaml\Yaml;
17+
use JsonStreamingParser\Parser;
18+
use JsonStreamingParser\Listener\InMemoryListener;
1719

1820
/**
1921
* Utility class to simplify reading JSON or YAML OpenAPI specs.
@@ -34,9 +36,17 @@ class Reader
3436
* The type of the returned object depends on the `$baseType` argument.
3537
* @throws TypeErrorException in case invalid spec data is supplied.
3638
*/
37-
public static function readFromJson(string $json, string $baseType = OpenApi::class): SpecObjectInterface
39+
public static function readFromJson(string $filename, string $baseType = OpenApi::class): SpecObjectInterface
3840
{
39-
return new $baseType(json_decode($json, true));
41+
$fp = fopen($filename, 'r');
42+
$listener = new InMemoryListener();
43+
$parser = new Parser($fp, $listener);
44+
$parser->parse();
45+
fclose($fp);
46+
47+
$data = $listener->getJson();
48+
49+
return new $baseType($data);
4050
}
4151

4252
/**
@@ -83,21 +93,15 @@ public static function readFromYaml(string $yaml, string $baseType = OpenApi::cl
8393
*/
8494
public static function readFromJsonFile(string $fileName, string $baseType = OpenApi::class, $resolveReferences = true): SpecObjectInterface
8595
{
86-
$fileContent = file_get_contents($fileName);
87-
if ($fileContent === false) {
88-
$e = new IOException("Failed to read file: '$fileName'");
89-
$e->fileName = $fileName;
90-
throw $e;
91-
}
92-
$spec = static::readFromJson($fileContent, $baseType);
96+
$spec = static::readFromJson($fileName, $baseType);
9397
$context = new ReferenceContext($spec, $fileName);
9498
$spec->setReferenceContext($context);
9599
if ($resolveReferences !== false) {
96100
if (is_string($resolveReferences)) {
97101
$context->mode = $resolveReferences;
98102
}
99103
if ($spec instanceof DocumentContextInterface) {
100-
$spec->setDocumentContext($spec, new JsonPointer(''));
104+
// $spec->setDocumentContext($spec, new JsonPointer(''));
101105
}
102106
$spec->resolveReferences();
103107
}

tests/IssueTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public function test224FailsOnLargeDefinitions()
1818
$openapi = Reader::readFromJsonFile(__DIR__.'/data/issue/224/cloudflare.json');
1919
$this->readLargeFiles($openapi);
2020

21-
$openapiYml = Reader::readFromYamlFile(__DIR__.'/data/issue/224/cloudflare.yml');
22-
$this->readLargeFiles($openapiYml);
21+
// $openapiYml = Reader::readFromYamlFile(__DIR__.'/data/issue/224/cloudflare.yml');
22+
// $this->readLargeFiles($openapiYml);
2323
}
2424

2525
private function readLargeFiles(OpenApi $openapi): void

0 commit comments

Comments
 (0)