Forum Discussion

galovics's avatar
galovics
New Contributor
4 years ago
Solved

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 ...
  • HKosova's avatar
    4 years ago

    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!