diff --git a/src/lib/openapi/PropertySchema.php b/src/lib/openapi/PropertySchema.php index 56d6df7a..227e2011 100644 --- a/src/lib/openapi/PropertySchema.php +++ b/src/lib/openapi/PropertySchema.php @@ -273,8 +273,10 @@ public function getSelfTargetProperty():?PropertySchema public function isRefPointerToSchema():bool { return $this->refPointer && - ((strpos($this->refPointer, self::REFERENCE_PATH) === 0) || - (str_ends_with($this->uri, '.yml')) || (str_ends_with($this->uri, '.yaml'))); + ( + (strpos($this->refPointer, self::REFERENCE_PATH) === 0) || + (str_ends_with($this->uri, '.yml')) || (str_ends_with($this->uri, '.yaml')) || (str_ends_with($this->uri, '.json')) + ); } public function isRefPointerToSelf():bool @@ -310,7 +312,7 @@ public function getRefSchemaName():string $pattern = strpos($this->refPointer, '/properties/') !== false ? '~^'.self::REFERENCE_PATH.'(?.+)/properties/(?.+)$~' : '~^'.self::REFERENCE_PATH.'(?.+)$~'; - $separateFilePattern = '/((\.\/)*)(?.+)(\.)(yml|yaml)(.*)/'; # https://github.com/php-openapi/yii2-openapi/issues/74 + $separateFilePattern = '/((\.\/)*)(?.+)(\.)(yml|yaml|json)(.*)/'; # https://github.com/php-openapi/yii2-openapi/issues/74 if (!\preg_match($pattern, $this->refPointer, $matches)) { if (!\preg_match($separateFilePattern, $this->uri, $separateFilePatternMatches)) { throw new InvalidDefinitionException('Invalid schema reference'); diff --git a/tests/specs/issue_fix/87_implement_for_json_in_is_ref_pointer_to_schema/Product.json b/tests/specs/issue_fix/87_implement_for_json_in_is_ref_pointer_to_schema/Product.json new file mode 100644 index 00000000..91514ab4 --- /dev/null +++ b/tests/specs/issue_fix/87_implement_for_json_in_is_ref_pointer_to_schema/Product.json @@ -0,0 +1,22 @@ +{ + "title": "Product", + "x-table": "products", + "type": "object", + "required": [ + "id", + "vat_rate" + ], + "properties": { + "id": { + "type": "integer" + }, + "vat_rate": { + "type": "string", + "enum": [ + "standard", + "none" + ], + "default": "standard" + } + } +} \ No newline at end of file diff --git a/tests/specs/issue_fix/87_implement_for_json_in_is_ref_pointer_to_schema/index.json b/tests/specs/issue_fix/87_implement_for_json_in_is_ref_pointer_to_schema/index.json new file mode 100644 index 00000000..a879d24c --- /dev/null +++ b/tests/specs/issue_fix/87_implement_for_json_in_is_ref_pointer_to_schema/index.json @@ -0,0 +1,39 @@ +{ + "openapi": "3.0.0", + "info": { + "version": "1.0.0", + "title": "\\#87" + }, + "paths": { + "/": { + "get": { + "responses": { + "200": { + "description": "The information" + } + } + } + } + }, + "components": { + "schemas": { + "Invoice": { + "type": "object", + "required": [ + "vat_rate" + ], + "properties": { + "id": { + "type": "integer" + }, + "vat_rate": { + "$ref": "./Product.json#/properties/vat_rate" + } + } + }, + "Product": { + "$ref": "./Product.json" + } + } + } +} \ No newline at end of file diff --git a/tests/specs/issue_fix/87_implement_for_json_in_is_ref_pointer_to_schema/index.php b/tests/specs/issue_fix/87_implement_for_json_in_is_ref_pointer_to_schema/index.php new file mode 100644 index 00000000..0fd4dc27 --- /dev/null +++ b/tests/specs/issue_fix/87_implement_for_json_in_is_ref_pointer_to_schema/index.php @@ -0,0 +1,13 @@ + '@specs/issue_fix/87_implement_for_json_in_is_ref_pointer_to_schema/index.json', + 'generateUrls' => false, + 'generateModels' => true, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => false, + 'generateMigrations' => true, + 'generateModelFaker' => true, +]; diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index 9d20ae4d..08d1a00c 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -1029,4 +1029,19 @@ public function test23ConsiderOpenapiExtensionXNoRelationAlsoInOtherPertinentPla $this->checkFiles($actualFiles, $expectedFiles); $this->runActualMigrations(); } + + // https://github.com/php-openapi/yii2-openapi/issues/87 + public function test87ImplementForJsonInIsrefpointertoschema() + { + $testFile = Yii::getAlias("@specs/issue_fix/87_implement_for_json_in_is_ref_pointer_to_schema/index.php"); + $this->runGenerator($testFile); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/74_invalid_schema_reference_error/mysql"), [ # this is intentional + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + $this->runActualMigrations(); + } }