Forum Discussion

galovics's avatar
galovics
New Contributor
5 years ago

allOf with same property names

Dear all,

 

I was wondering if its allowed to use the same property name within a composed definition.

Think about the following, a schema that is using allOf and is composed by 2 subschemas, for example OrderInfo and Category. Both OrderInfo and Category has a common attribute: "id". 

Is this allowed by the OpenAPI spec? I was trying to find this but wasn't able to do so. A clarification would be appreciated.

Thanks!

  • Yes this is allowed. allOf does not actually mean "merge" (even though some tools handle it as "merge"). It means that an instance is considered valid if it matches each of the listed subschemas independently.

     

    Consider this schema:

    type: object
    required: [id]
    allOf:
      - properties:
          id:
            type: integer
      - properties:
          id:
            minimum: 0

     {"id": 5} is a valid - because the "id" value (5) matches both "type: integer" and "minimum: 0".

    But {"id": -8} is invalid - because it does not match the "mininum: 0" constraint defined in the second subschema.

     

    Because the subschemas in allOf know nothing about each other, it's also possible to create "impossible" schemas with contradicting constraints. For example, this schema does not match anything because "id" cannot be both an integer and a string:

    type: object
    required: [id]
    allOf:
      - properties:
          id:
            type: integer
      - properties:
          id:
            type: string

     

    Hope this answers your question!

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Yes this is allowed. allOf does not actually mean "merge" (even though some tools handle it as "merge"). It means that an instance is considered valid if it matches each of the listed subschemas independently.

     

    Consider this schema:

    type: object
    required: [id]
    allOf:
      - properties:
          id:
            type: integer
      - properties:
          id:
            minimum: 0

     {"id": 5} is a valid - because the "id" value (5) matches both "type: integer" and "minimum: 0".

    But {"id": -8} is invalid - because it does not match the "mininum: 0" constraint defined in the second subschema.

     

    Because the subschemas in allOf know nothing about each other, it's also possible to create "impossible" schemas with contradicting constraints. For example, this schema does not match anything because "id" cannot be both an integer and a string:

    type: object
    required: [id]
    allOf:
      - properties:
          id:
            type: integer
      - properties:
          id:
            type: string

     

    Hope this answers your question!

    • galovics's avatar
      galovics
      New Contributor

      Thank you for the clarification! Its clear now.

      • slogic's avatar
        slogic
        Occasional Contributor

        is oneOf supported under allOf?