Skip to content

Implement for json in isRefPointerToSchema() #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/lib/openapi/PropertySchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,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
Expand Down Expand Up @@ -305,7 +307,7 @@ public function getRefSchemaName():string
$pattern = strpos($this->refPointer, '/properties/') !== false ?
'~^'.self::REFERENCE_PATH.'(?<schemaName>.+)/properties/(?<propName>.+)$~'
: '~^'.self::REFERENCE_PATH.'(?<schemaName>.+)$~';
$separateFilePattern = '/((\.\/)*)(?<schemaName>.+)(\.)(yml|yaml)(.*)/'; # https://github.com/php-openapi/yii2-openapi/issues/74
$separateFilePattern = '/((\.\/)*)(?<schemaName>.+)(\.)(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');
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
'openApiPath' => '@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,
];
15 changes: 15 additions & 0 deletions tests/unit/IssueFixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1014,4 +1014,19 @@ public function test22BugRulesRequiredIsGeneratedBeforeDefault()
]);
$this->checkFiles($actualFiles, $expectedFiles);
}

// 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();
}
}