I am trying to create a REST service by describing it in Swagger YAML.
The service has three paths:
My current YAML file to describe these paths looks like this:
swagger: '2.0'
info:
version: '0.0.1'
title: Test API
host: api.test.com
basePath: /
schemes:
- https
consumes:
- application/json
produces:
- application/json
paths:
/versions:
post:
responses:
'201':
description: Returns all versions.
default:
description: unexpected error
/partners/{partnerId}/users/{userId}/sessions:
parameters:
- name: partnerId
in: path
type: integer
- name: userId
in: path
type: string
post:
responses:
'201':
description: Returns a UserSession object with info about the user session.
default:
description: unexpected error
/partners/{partnerId}/books/{bookId}/:
parameters:
- name: partnerId
in: path
type: integer
- name: bookId
in: path
type: string
get:
responses:
'200':
description: Gets a book.
default:
description: unexpected error
In this YAML file the parameter "partnerId" is declared twice.
Is there a way to make "subpaths" such that I don't have to declare the /partners/{partnerId}
part of the path twice?
What you can do is declare the parameter at the top level, and then refer to it.
swagger: '2.0'
info:
version: '0.0.1'
title: Test API
host: api.test.com
basePath: /
schemes:
- https
consumes:
- application/json
produces:
- application/json
parameters:
partnerId:
name: partnerId
in: path
type: integer
paths:
/versions:
post:
responses:
'201':
description: Returns all versions.
default:
description: unexpected error
/partners/{partnerId}/users/{userId}/sessions:
parameters:
- $ref: '#/parameters/partnerId'
- name: userId
in: path
type: string
post:
responses:
'201':
description: Returns a UserSession object with info about the user session.
default:
description: unexpected error
/partners/{partnerId}/books/{bookId}/:
parameters:
- $ref: '#/parameters/partnerId'
- name: bookId
in: path
type: string
get:
responses:
'200':
description: Gets a book.
default:
description: unexpected error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With