Skip to content

Commit 05e608f

Browse files
committed
Refactor JSON schema handling for Laravel 12+ compatibility
1 parent c238a99 commit 05e608f

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed

src/Commands/GraphqlGenerateCommand.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -344,23 +344,21 @@ protected function mapFieldToGraphQLType($field, bool $isInput = false): string
344344
$fieldClass = get_class($field);
345345
$fieldClassName = class_basename($fieldClass);
346346

347-
// Use the field's built-in type guessing if available
348-
if (method_exists($field, 'guessFieldType')) {
347+
// Use the field's built-in type guessing if available (Laravel 12+ only)
348+
if (method_exists($field, 'guessFieldType') && method_exists($field, 'hasJsonSchemaSupport') && $field::hasJsonSchemaSupport()) {
349349
$fieldType = $field->guessFieldType(app(RepositoryStoreRequest::class));
350350

351-
switch ($fieldType) {
352-
case 'boolean':
353-
return 'Boolean';
354-
case 'number':
355-
case 'integer':
356-
return 'Int';
357-
case 'array':
358-
return $isInput ? '[String!]' : '[String!]';
359-
case 'object':
360-
return $isInput ? 'JSON' : 'JSON';
361-
case 'string':
362-
default:
363-
return 'String';
351+
if ($fieldType !== null) {
352+
// Check the Type object class to determine GraphQL type
353+
$typeClass = class_basename(get_class($fieldType));
354+
355+
return match ($typeClass) {
356+
'BooleanType' => 'Boolean',
357+
'NumberType', 'IntegerType' => 'Int',
358+
'ArrayType' => $isInput ? '[String!]' : '[String!]',
359+
'ObjectType' => $isInput ? 'JSON' : 'JSON',
360+
default => 'String',
361+
};
364362
}
365363
}
366364

src/MCP/Concerns/FieldMcpSchemaDetection.php

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,32 @@
66
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
77
use Binaryk\LaravelRestify\MCP\Actions\JsonSchemaFromRulesAction;
88
use Binaryk\LaravelRestify\Repositories\Repository;
9-
use Illuminate\JsonSchema\JsonSchema;
10-
use Illuminate\JsonSchema\JsonSchemaTypeFactory;
11-
use Illuminate\JsonSchema\Types\ArrayType;
12-
use Illuminate\JsonSchema\Types\BooleanType;
13-
use Illuminate\JsonSchema\Types\NumberType;
14-
use Illuminate\JsonSchema\Types\Type;
159

1610
/**
1711
* @mixin \Binaryk\LaravelRestify\Fields\Field
1812
*/
1913
trait FieldMcpSchemaDetection
2014
{
15+
/**
16+
* Check if Laravel 12+ JsonSchema classes are available.
17+
*/
18+
public static function hasJsonSchemaSupport(): bool
19+
{
20+
return class_exists(\Illuminate\JsonSchema\JsonSchemaTypeFactory::class);
21+
}
22+
2123
/**
2224
* Guess the field type based on validation rules, field class, and attribute patterns.
25+
*
26+
* @return \Illuminate\JsonSchema\Types\Type|null Returns Type on Laravel 12+, null on Laravel 11
2327
*/
24-
public function guessFieldType(RestifyRequest $request): Type
28+
public function guessFieldType(RestifyRequest $request): mixed
2529
{
26-
$schema = new JsonSchemaTypeFactory;
30+
if (! static::hasJsonSchemaSupport()) {
31+
return null;
32+
}
33+
34+
$schema = new \Illuminate\JsonSchema\JsonSchemaTypeFactory;
2735

2836
$rules = $this->getRulesForRequest($request);
2937

@@ -84,8 +92,8 @@ public function getDescription(RestifyRequest $request, Repository $repository):
8492
}
8593
}
8694

87-
// Add examples based on field type and name
88-
if ($this->jsonSchema instanceof Type) {
95+
// Add examples based on field type and name (Laravel 12+ only)
96+
if (static::hasJsonSchemaSupport() && $this->jsonSchema instanceof \Illuminate\JsonSchema\Types\Type) {
8997
$examples = $this->generateFieldExamples($this->jsonSchema);
9098

9199
if (! empty($examples)) {
@@ -140,20 +148,22 @@ protected function formatValidationRules(array $rules): array
140148

141149
/**
142150
* Generate examples for the field.
151+
*
152+
* @param \Illuminate\JsonSchema\JsonSchema $fieldType
143153
*/
144-
protected function generateFieldExamples(JsonSchema $fieldType): array
154+
protected function generateFieldExamples(mixed $fieldType): array
145155
{
146156
$attribute = strtolower($this->attribute);
147157

148-
if ($fieldType instanceof BooleanType) {
158+
if ($fieldType instanceof \Illuminate\JsonSchema\Types\BooleanType) {
149159
return ['true', 'false'];
150160
}
151161

152-
if ($fieldType instanceof NumberType) {
162+
if ($fieldType instanceof \Illuminate\JsonSchema\Types\NumberType) {
153163
return $this->getNumberExamples($attribute);
154164
}
155165

156-
if ($fieldType instanceof ArrayType) {
166+
if ($fieldType instanceof \Illuminate\JsonSchema\Types\ArrayType) {
157167
return ['["item1", "item2"]', '{"key": "value"}'];
158168
}
159169

@@ -278,8 +288,11 @@ protected function hasAnyRule(array $ruleStrings, array $rulesToCheck): bool
278288

279289
/**
280290
* Guess type from attribute name patterns.
291+
*
292+
* @param \Illuminate\JsonSchema\JsonSchema $schema
293+
* @return \Illuminate\JsonSchema\Types\Type|null
281294
*/
282-
protected function guessTypeFromAttributeName(JsonSchema $schema): ?Type
295+
protected function guessTypeFromAttributeName(mixed $schema): mixed
283296
{
284297
$attribute = $this->attribute;
285298

0 commit comments

Comments
 (0)