iamjoshua
iamjoshua

Reputation: 1269

How to define two different responses for same Response code with 302 Redirect?

I'm trying to set two different responses of 302 in out API since our API redirects to a different Endpoint depending on the request sent. I'm current using Open API ver 3.0.1. How do I do that?

I basically just want two different Location Headers for the 302 redirect describing how why the particular Location header is returned. Or have two same 302 response codes

I basically just want two different Location Headers for the 302 redirect describing how why the particular Location header is returned

I was following this older thread. But it doesn't correctly reference the two different Location headers in the actual API Endpoint, and instead just shows a blank Location header.

My Code snippet is:

  responses:
    '200':
      description: 'Sample 200 Response'
    '302':
      description: 'Sample 302 Responses. See <302ExpiredRefreshTokenstring> & <302ExpiredUserRefreshTokenstring> schemas for reference'
      headers:
        Location:
          schema:
            oneOf:
              - $ref: '#/components/schemas/302ExpiredRefreshToken'
              - $ref: '#/components/schemas/302ExpiredUserRefreshToken'

And the schema is:

components:
  schemas:
    302ExpiredRefreshToken:
      type: string
      example: 'www.<UIEndpoint>/ref-login?SessionId=d3fe5c6959ae0ce502d6027a7693c3ebe4543f51a878d60004e1331723fc0187&redirect_uri=https://www.lazada.com.ph&state=sample_state_54321&refresh_token=sCY2O0kev5Ic516A1INnQwRc8D1hbuOJ&BillerCode=00494&UserId=JBC_28'
      description: Redirect URI if Refresh Token (FinTech) has already expired.
    302ExpiredUserRefreshToken:
      type: string
      example: 'www.<UIEndpoint>/cpw-login?SessionId=d3fe5c6959ae0ce502d6027a7693c3ebe4543f51a878d60004e1331723fc0187&redirect_uri=https://www.lazada.com.ph&state=sample_state_54321&refresh_token=sCY2O0kev5Ic516A1INnQwRc8D1hbuOJ&BillerCode=00494&UserId=JBC_28'
  description: Redirect URI if User Refresh Token (DigiFi) has already expired. This could be triggered even if the Refresh Token (FinTech) has not expired yet.

Adding multiple examples doesn't work either as it doesn't show the multiple examples, instead it's just a blank

headers:
            Location:
              schema:
                type: string
              examples: 
                'test1':
                  value: 'value1'
                'test2':
                  value: 'value2'
              description: 'Description of Location Header'

But the Multiple examples for a header work when placed on the request paramters as I added examples in the request like this.

 parameters:
          name: Location
          in: header
          examples:       
            test1:         
              value: 'value2'   
              summary: 'summary 1'
            test2: 
              value: 'value 1'  
              summary: 'summary 2' 

Upvotes: 4

Views: 8938

Answers (1)

Helen
Helen

Reputation: 98022

There's no need for multiple schemas for the Location header because the data type is the same in both cases - type: string. Instead, add multiple examples of the header value:

      responses:
        ...
        '302':
          description: Some token has expired... Clients should follow the `Location` header to refresh the token.
          headers:
            Location:
              description: URI where the client can refresh the expired token.
              schema:
                type: string
                format: uri  # Optional - use if the Location header is an absolute URI, starting with http(s)://
              examples:
                302ExpiredRefreshToken:
                  description: Redirect URI if Refresh Token (FinTech) has already expired
                  value: 'www.<UIEndpoint>/ref-login?SessionId=d3fe5c6959ae0ce502d6027a7693c3ebe4543f51a878d60004e1331723fc0187&redirect_uri=https://www.lazada.com.ph&state=sample_state_54321&refresh_token=sCY2O0kev5Ic516A1INnQwRc8D1hbuOJ&BillerCode=00494&UserId=JBC_28'
                302ExpiredUserRefreshToken:
                  description: >-
                    Redirect URI if User Refresh Token (DigiFi) has already expired.
                    This could be triggered even if the Refresh Token (FinTech) has not expired yet.
                  value: 'www.<UIEndpoint>/cpw-login?SessionId=d3fe5c6959ae0ce502d6027a7693c3ebe4543f51a878d60004e1331723fc0187&redirect_uri=https://www.lazada.com.ph&state=sample_state_54321&refresh_token=sCY2O0kev5Ic516A1INnQwRc8D1hbuOJ&BillerCode=00494&UserId=JBC_28'

But since you use Swagger UI, please note that it currently does not display examples in response headers. Follow this issue for updates: https://github.com/swagger-api/swagger-ui/issues/5432

Upvotes: 4

Related Questions