Skip to content
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

oneOf discriminator validation #229

Open
leon0399 opened this issue Aug 29, 2024 · 1 comment
Open

oneOf discriminator validation #229

leon0399 opened this issue Aug 29, 2024 · 1 comment

Comments

@leon0399
Copy link

Package version
0.22.0

Describe the bug
Schema discriminator is not supported: https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/

To Reproduce

components:
  responses:
    sampleObjectResponse:
      content:
        application/json:
          schema:
            oneOf:
              - $ref: '#/components/schemas/simpleObject'
              - $ref: '#/components/schemas/complexObject'
            discriminator:
              propertyName: objectType
  
  schemas:
    simpleObject:
      type: object
      required:
        - objectType
      properties:
        objectType:
          type: string
      
    complexObject:
      type: object
      required:
        - objectType
      properties:
        objectType:
          type: string
      

Expected behaviour
The validator must find a schema based on the discriminator

Additional context
I've tried to make a fix myself, but does not know how to resolve the schema by ref from the validator:
src/Schema/Keywords/OneOf.php:59

        // Validate against all schemas
        $schemaValidator = new SchemaValidator($this->validationDataType);
        $innerExceptions = [];
        $validSchemas    = [];

+        $discriminator = $this->parentSchema->discriminator;
+        if ($discriminator !== null) {
+            $resolved = $discriminator->mapping[$data[$discriminator->propertyName]];
+            // how do I get ref by Schema object? or how to get schema by ref?
+            // $oneOf = array_filter(
+            //     $oneOf,
+            //     static fn(CebeSchema $schema) => $schema->getRef() === $resolved
+            // );
+        }

        foreach ($oneOf as $schema) {
            try {
                $schemaValidator->validate($data, $schema, $this->dataBreadCrumb);
                $validSchemas[] = $schema;
            } catch (SchemaMismatch $e) {
                $innerExceptions[] = $e;
            }
        }
@leon0399
Copy link
Author

A temporary solution that even might be better in some way:

Instead of relying on discriminators based on property, I assigned this property a schema of enum with one entry:

components:
  responses:
    sampleObjectResponse:
      content:
        application/json:
          schema:
            oneOf:
              - $ref: '#/components/schemas/simpleObject'
              - $ref: '#/components/schemas/complexObject'
-            discriminator:
-              propertyName: objectType
  …
  schemas:
    simpleObject:
      type: object
      required:
        - objectType
      properties:
        objectType:
          type: string
+          enum: 
+            - simpleObject
      …
    complexObject:
      type: object
      required:
        - objectType
      properties:
        objectType:
          type: string
+          enum: 
+            - complexObject

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

No branches or pull requests

1 participant