cancel
Showing results for 
Search instead for 
Did you mean: 

Partial use of model attributes

SOLVED
Highlighted
New Contributor

Partial use of model attributes

Good afternoon!

Please tell me what to do if in requestBody or response it is necessary to transfer not all attributes of the entity, but only the necessary ones. For example, there is a certain entity:

Entity:
      title: Entity
      type: object
      properties:
        id:
          type: integer
          format: int 64
          example: 6421
        attributes2:
          type: string
          description: text
        attributes3:
          type: string
          description: "text"
        attributes4:
          type: array
          description: "text"
          items: 
            $ref: '#/components/schemas/Schema'            
        attributes5:
           type: boolean
           description: "text"

For the post method, you need to pass only part of this entity in the request, for example:

Entity:
      title: Entity
      type: object
      properties:
        attributes2:
          type: string
          description: text
        attributes3:
          type: string
          description: "text"

How to do this so that requestBody does not specify completely, but only with the necessary attributes?

Now it turns out to me that the whole entity is indicated, for example:

post:
     tags:
     - Entities
     summary: ""
     description: "text"
     operationId: addEntity
     requestBody:
       description: "text"
       required: true
       content:
         application/json:
           schema:
             $ref: '#/components/schemas/Entity'        
     responses:
        '201':
           description: Created
           content:
               application/json:
                schema:
                 type: object
                 properties:
                  data:
                   $ref: '#/components/schemas/Entity'

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Moderator

Re: Partial use of model attributes

Hi Maxim,

 

It's not possible to exclude properties from a schema. You need to do it the other way round - define a base schema with just the common properties, then inherit from that schema and add new properties.


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
Highlighted
Moderator

Re: Partial use of model attributes

Check out this thread: https://community.smartbear.com/t5/Swagger-Open-Source-Tools/Component-schema-reuse-with-different-v...


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: Partial use of model attributes

It is recommended to use 'allOf' here to add properties, but how to exclude attributes from an entity that contains all possible attributes?

Highlighted
Moderator

Re: Partial use of model attributes

Hi Maxim,

 

It's not possible to exclude properties from a schema. You need to do it the other way round - define a base schema with just the common properties, then inherit from that schema and add new properties.


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: Partial use of model attributes

Thanks, okay.

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