Discriminator with allOf return not allowed error validation parent properties

Issue #396 new
Ellyx Christian created an issue

We are using discriminator to be able to set different different type of object in a property, we are using the following specification

{
  "components": {
    "schemas": {
      "Party": {
        "properties": {
          "name": {
            "type": "string"
          },
          "contactMediums": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ContactMedium"
            }
          }
        }
      },
      "ContactMedium": {
        "title": "ContactMedium",
        "required": [
          "@type"
        ],
        "type": "object",
        "properties": {
          "@type": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "description": {
            "type": "string"
          }
        },
        "discriminator": {
          "propertyName": "@type",
          "mapping": {
            "EmailContact": "EmailContact"
          }
        }
      },
      "EmailContact": {
        "title": "EmailContact",
        "allOf": [
          {
            "$ref": "#/components/schemas/ContactMedium"
          },
          {
            "type": "object",
            "properties": {
              "emailAddress": {
                "type": "string"
              }
            }
          }
        ]
      }
    }
  }
}

Validating party like below

{
  "name": "John",
  "contactMediums": [
    {
      "@type": "EmailContact",
      "name": "Primary Email",
      "emailAddress": "test@example.com"
    }
  ]
}

caused the following validation result:

[Path '/contactMediums/0'] Object instance has properties which are not allowed by the schema: ["@type","name"]

i created the validator using this code

final ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolveFully(true);
parseOptions.setResolveCombinators(true);

final OpenApiInteractionValidator validator = OpenApiInteractionValidator
    .createForInlineApiSpecification(this.specString)
    .withParseOptions(parseOptions)
    .build();

Comments (3)

  1. Peter Streef

    I think I have the same problem.

    Product:
      required:
      - "name"
      - "type"
      type: "object"
      properties:
        name:
          type: "string"
        type:
          type: "string"
          enum:
          - "a"
          - "b"
          - "c"
      discriminator:
        propertyName: "type"
        mapping:
          a: "#/components/schemas/A"
          b: "#/components/schemas/B"
          c: "#/components/schemas/C"
    
    A:
      required:
      - "name"
      - "other"
      type: "object"
      allOf:
      - $ref: "#/components/schemas/Product"
      - type: "object"
        properties:
          other:
            type: "string"
    

    is failing with:

    Object instance has properties which are not allowed by the schema: ["name","type"]

    Explicitly adding the fields to A works, but with the `allOf` it should not be required.

  2. tomasz.trebski

    Also present here with 2.35.1 and OAS 3.1.0.
    I cannot make a use of discriminator and allOf together

  3. Sravan Kumar Sandela

    I see lot of issues around Discriminator with this library. The issues have been raised since 2020, why is this not being fixed? @James Navin what is your take on lack of Discriminator support for oneOf and anyOf, and when is this TODO going to be addressed?

     // TODO: `oneOf` and `anyOf` composition validation logic
            final JsonNode currentSchemaNode = data.getSchema().getNode();
            if (currentSchemaNode.has("oneOf") || currentSchemaNode.has("anyOf")) {
                log.debug("Support for discriminators with oneOf/anyOf not implemented yet. Validation may be inaccurate.");
                return;
            }
    

  4. Log in to comment