Ask a Question

I want to create a component one with default value other one without default value how to do

SOLVED
kashira
New Contributor

I want to create a component one with default value other one without default value how to do

G'day All, 

 

Please can someone suggest me what is best way to write this component with reuse. 

 

Title:

  Type: string 

   Enum: 

       - Mr

       - Mrs

       - Unknown

 

TitleWithDefault

   $ref: '#/components/schemas/Title'

   default: Unknown

 

Is it possible?

 

Please if someone knows the answer can you share it.

 

Thanking you, 

Raj

6 REPLIES 6
HKosova
SmartBear Alumni (Retired)

Hi Raj, try wrappin the $ref into allOf:

TitleWithDefault
  allOf:
    - $ref: '#/components/schemas/Title'
  default: Unknown

 


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

G'day Helen,

 

Thanks for your reply. I tried. It did not work. 

 

kashira_0-1622128236093.png

Please can you help.

 

Thanking you,

Raj

HKosova
SmartBear Alumni (Retired)

"default" needs to be on the same level as "allOf", not inside it.

TitleWithDefault
  allOf:
    - $ref: '#/components/schemas/Title'
  default: Unknown

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

G'day, 

 

I am not sure I followed as you suggested  - it still did not work.

 

kashira_0-1622547567102.png

Regards, 

HKosova
SmartBear Alumni (Retired)

Can you please post your entire "components" section with all schemas?


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

G'day Helen, 

 

Thanks so much for spending time to help me with this. I posting the complete yml. 

 

openapi: 3.0.0

info:
  title: Customers API
  description: Customer resource
  version: 1.0.0
  contact:
    name: Raj
    email: Rajendra.Kashi@gmail.com

paths:
  /customers:
    get:
      description: to retireve all the customer detail
      summary: This route retrieves all the customer detail
      responses:
        200:
          description: retieves all the customer information
          content: 
            application/json:
              schema:
                $ref: '#/components/schemas/CustomersRetrieveResponse'
  /customers/{customer_id}:
    get: 
      description: retrieve customer
      summary: This route retrieve customer by customer id
      parameters:
        - in: path
          name: customer_id
          required:  true
          description:  customer id
          schema:
            type:  string
      responses:
        200:
          description: retieves all the customer information
          content: 
            application/json:
              schema:
                $ref: '#/components/schemas/CustomerRetrieveResponse'            
    post: 
      description: create customer
      summary: This route retrieves all the customer detail
      requestBody:
        required: false
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CustomerCreateRequest'
      responses:        
        200:
          description: Description
          content: 
            application/json:
              schema:
                type: string   

components:
  schemas:      
    CustomersRetrieveResponse: 
      type: array
      items: 
        $ref: '#/components/schemas/CustomerRetrieveItem'
    
    CustomerRetrieveResponse: 
      $ref: '#/components/schemas/CustomerRetrieveItem'

    CustomerCreateRequest:
      $ref: '#/components/schemas/CustomerCreateItem'

    CustomerCreateItem:
      required: 
        - first_name
        - last_name
      allOf:
        - $ref: '#/components/schemas/CustomerTitleWithDefault'
        - $ref: '#/components/schemas/CustomerBasicData'  

    CustomerRetrieveItem:
      allOf:
        - $ref: '#/components/schemas/CustomerId'
        - $ref: '#/components/schemas/CustomerTitle'
        - $ref: '#/components/schemas/CustomerBasicData'

    CustomerBasicData:
      type: object
      properties: 
        first_name: 
          type: string 
        last_name:
          type: string

    CustomerId:
      type: object
      properties:
        id: 
          type: string


    CustomerTitleWithDefault:
      allOf:
        - $ref: '#/components/schemas/CustomerTitle'
      default: Unknown   
      
      #type: object
      #properties: 
      #  title: 
      #    type: string
      #    enum: 
      #      - Mr
      #      - Mrs
      #      - M/S
      #      - Dr
      #      - Unknown
      #    default: Unknown

    CustomerTitle:
      type: object
      properties: 
        title: 
          type: string
          enum: 
            - Mr
            - Mrs
            - M/S
            - Dr
            - Unknown
 
 
Thanking you, 
Raj
cancel
Showing results for 
Search instead for 
Did you mean: