swaggeropenapidredd

How to provide separate examples for each HTTP status code?


I run into problem while trying to instruct Dredd to issue a different requests to trigger two distinct scenarios: success with code 201 and failure with code 400.

I tried to setup a separate example per HTTP status code but couldn't manage to do that. I can add example section in requestBody but then it will be used in both example - for success and failure.

openapi: 3.0.2
info:  
  description: Body example per HTTP code illustration  
  version: 1.0.0  
  title: Profile 
tags:
  - name: profile
    description: User profiles
paths:  
  /profiles:
    post:     
      tags:        
        - profiles
      summary: Create profile
      operationId: createProfile
      requestBody:        
        description: Create Profile
        content:          
          application/json:            
            schema:              
              $ref: '#/components/schemas/CreateProfile'
        required: true      
      responses:        
        '201':          
          description: Successful operation          
          headers:            
            Location:              
              schema:                
                type: string                
                description: Profile location        
        '400':          
          description: Profile is invalid (missing fields/null values)
          content:            
            application/json:              
              schema:                
                $ref: '#/components/schemas/ProfileCreationError'              
              examples:                
                failing_scenrio:                  
                  summary: An example tailored to work with Dredd                  
                  value:                   
                    name: can't be blank                    
                    email: can't be blank
components:  
  schemas:    
    CreateProfile:      
      type: object      
      required:        
        - name        
        - email        
        - age     
      properties:        
        name:          
          type: string        
        email:          
          type: string        
        age:          
          type: integer          
          format: int32          
          minimum: 0    
    ProfileCreationError:      
      type: object      
      properties:        
        name:          
          type: string        
        email:          
          type: string        
        age:          
          type: integer

I would like to be able to run tests for both HTTP codes: 201 and 400. Bonus points for an example of how to do same thing with path param. For example, to provide both found and not found examples for /profiles/{id} (i.e. 200 and 404).


Solution

  • The OpenAPI 3 support is currently (July 2019, Dredd v11.2.9) experimental and this particular behavior is still undefined. The problem is tracked as a GitHub issue #1257. I recommend you to subscribe to the issue to see when it gets resolved or consider contributing a solution.