cancel
Showing results for 
Search instead for 
Did you mean: 

allOf with same property names

SOLVED
Highlighted
New Contributor

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Moderator

Re: allOf with same property names

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!


Helen Kosova
SmartBear Documentation Team Lead
________________________

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

2 REPLIES 2
Highlighted
Moderator

Re: allOf with same property names

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!


Helen Kosova
SmartBear Documentation Team Lead
________________________

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

Highlighted
New Contributor

Re: allOf with same property names

Thank you for the clarification! Its clear now.

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors
Top Kudoed Posts