Skip to content

fix: map JSON Schema enums to EnumSchema - #39

Open
dimabullet wants to merge 1 commit into
prism-php:mainfrom
dimabullet:fix/enum-schema-from-json-schema
Open

fix: map JSON Schema enums to EnumSchema#39
dimabullet wants to merge 1 commit into
prism-php:mainfrom
dimabullet:fix/enum-schema-from-json-schema

Conversation

@dimabullet

Copy link
Copy Markdown

Fixes #38.

getSchemeParameter() resolved a property by matching on type alone, so a standard JSON Schema enum ({"type": "string", "enum": [...]}) became a plain StringSchema and the allowed values never reached the model. The existing 'enum' branch expects {"type": "enum", "options": [...]}, which is not JSON Schema and which no MCP server emits, so it was unreachable.

This resolves the enum keyword before the type based matching:

$options = data_get($property, 'enum');

if (is_array($options) && $options !== [] && $this->isEnumOptions($options)) {
    return new EnumSchema($name, $description, array_values($options));
}

Because the method recurses through $this for items, enums nested inside arrays are covered as well, without extra handling.

isEnumOptions() guards the case Prism cannot type: EnumSchema derives its own type from the options and only handles strings and numbers, so anything else falls through to the existing resolution rather than blowing up.

The old 'enum' branch is left untouched, so nothing that relied on it changes.

Tests

Added an enum_tool fixture to RelayFake and two cases to RelayTest: a string enum and an enum nested in array items. Full suite passes (90 tests), Pint and PHPStan are clean.

- resolve the enum keyword before falling back to type based matching
- keep enum options nested in array items
- cover string enums and array item enums with tests
@dimabullet
dimabullet force-pushed the fix/enum-schema-from-json-schema branch from 884d93d to 41120c0 Compare July 29, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enum constraints from MCP tool schemas are dropped

1 participant