cancel
Showing results for 
Search instead for 
Did you mean: 

Using objects from components in arrays of other object

Highlighted
New Contributor

Using objects from components in arrays of other object

Hello everyone. I can't find any solution, unfortunately. So, I have the object 'address' in my schemas of components, and I want to use it in the array 'addresses', which is a part of the request body. How can I do that? If I do:

 

addresses:
type: array
items:
$ref: '#/components/schemas/addresses'

 

then I get:

 

"addresses": [

{

"address": [

{

 

but I need just properties of 'address' in 'addresses' like:

"addresses":[{properties_of_address},{properties_of_address}]

Tags (3)
3 REPLIES 3
Highlighted
Moderator

Re: Using objects from components in arrays of other object

Can you also post the "address" schema?

Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Highlighted
New Contributor

Re: Using objects from components in arrays of other object

attached

Highlighted
Moderator

Re: Using objects from components in arrays of other object

Your schemas look correct. Make sure there are no extra nesting levels in the schemas. Check this example for the reference:

HKosova_0-1594910274826.png

 

openapi: 3.0.0
info:
  title: Array of objects
  version: 1.0.0
paths:
  /something:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/addresses'
      responses:
        200:
          description: OK

components:
  schemas:

    addresses:
      type: object
      properties:
        addresses:
          type: array
          items:
            $ref: '#/components/schemas/address'

    address:
      type: object
      description: A user's address
      properties:
        country:
          type: string
        country_subject:
          type: string

 


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
New Here?
Join us and watch the welcome video:
Top Kudoed Authors
Top Kudoed Posts