Not able to correctly validate a reference when it is in an allOf

Issue #389 new
Andreas Petersen created an issue

I am not able to correctly validate a reference when it is in an allOf.

swagger-request-validator-core version 2.28.2.

Validator configuration:

String openApiUrl = URI.create(...).toString();

ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolve(true);
parseOptions.setResolveFully(true);
parseOptions.setResolveCombinators(true);
parseOptions.setFlatten(true); // This don't make a difference if they are there are not
parseOptions.setFlattenComposedSchemas(true); // This don't make a difference if they are there are not

OpenApiInteractionValidator validator = OpenApiInteractionValidator
        .createForSpecificationUrl(openApiUrl)
        .withParseOptions(parseOptions)
        .build();

OpenAPI:

---
openapi: 3.0.3
info:
  title: Ref error
  description: Good description.
  version: v1
paths:
  /{id}:
    post:
      summary: Create item.
      operationId: postItem
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      requestBody:
        description: Create new item.
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/DescriptionItem'
        required: true
      responses:
        "201":
          description: Return created item.
components:
  schemas:
    DescriptionItem:
      allOf:
        - $ref: '#/components/schemas/Item'
        - type: object
          required:
            - description
          properties:
            description:
              description: A text description of the WealthItem.
              maxLength: 250
              minLength: 1
              type: string
              example: Alm. depot
    MyCategory:
      description: Some generic description.
      enum:
        - A
        - B
      type: string
      example: A
    Item:
      required:
        - classType
        - category
      type: object
      properties:
        classType:
          description: Identifier of discriminator mapping class.
          type: string
        category:
          description: My category
          allOf:
            - $ref: '#/components/schemas/MyCategory'
      discriminator:
        propertyName: classType
        mapping:
          DescriptionItem: '#/components/schemas/DescriptionItem'

Request body:

{
    "category": "A",
    "classType": "DescriptionItem",
    "description": "Good description"
}

Error:

[ERROR - Instance failed to match exactly one schema (matched 0 out of 1): [/components/schemas/id_body/oneOf/0: Instance type (string) does not match any allowed primitive type (allowed: ["object"])]
ERROR - [Path '/category'] Instance type (string) does not match any allowed primitive type (allowed: ["object"]): []]

The validator is not able to determine that MyCategory is of type string. If I add type: string to category:

category:
  description: My category
  type: string
  allOf:
    - $ref: '#/components/schemas/MyCategory'

then it works. Or, if I drop my description and reference directly:

category:
  $ref: '#/components/schemas/MyCategory'

then it also works. I have added a description as per this issue: https://github.com/OAI/OpenAPI-Specification/issues/2033

Comments (1)

  1. Log in to comment