cancel
Showing results for 
Search instead for 
Did you mean: 

allOf with same property names

SOLVED
galovics
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
HKosova
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

4 REPLIES 4
HKosova
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

galovics
New Contributor

Re: allOf with same property names

Thank you for the clarification! Its clear now.

slogic
Occasional Contributor

Re: allOf with same property names

is oneOf supported under allOf?

slogic
Occasional Contributor

Re: allOf with same property names

Ok, i found this https://community.smartbear.com/t5/Swagger-Open-Source-Tools/schema-via-oneOf-with-additional-common... thread. I suggest to update documentation to cleary state that statements can be nested.

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