Если мы посмотрим на API сообщений (например), я хочу, чтобы он мог
- создать сообщение
- получить сообщение
- обновить сообщение
Сообщение содержит внешнюю ссылку (уникальный идентификатор от потребителя)
- Этот external_id должен быть установлен при создании с запросом POST.
- Этот external_id нельзя было изменить с помощью PATCH.
Каково решение для его реализации?
Пример API:
swagger: '2.0'
host: api.com
basePath: /v2
schemes:
- https
info:
title: dummy
version: 1.0.0
consumes:
- application/json
produces:
- application/json
paths:
/messages:
post:
summary: Create a message
parameters:
- name: message
in: body
required: true
schema:
$ref: '#/definitions/Message'
responses:
201:
description: Ok
schema:
$ref: '#/definitions/Message'
/messages/{id}:
get:
summary: "Get a message by ID"
parameters:
- name: id
in: path
description: The message ID
required: true
type: string
format: uuid
responses:
200:
description: OK - the message
schema:
$ref: '#/definitions/Message'
patch:
summary: Modify a message
parameters:
- name: id
in: path
description: The message ID
required: true
type: string
format: uuid
- name: message
in: body
required: true
schema:
$ref: '#/definitions/Message'
responses:
201:
description: Ok
schema:
$ref: '#/definitions/Message'
definitions:
Message:
type: object
required:
- id
- external_id
- title
- content
- created_at
properties:
id:
type: string
readOnly: true
external_id:
type: string
description: "Your own reference ID"
title:
type: string
content:
type: string
created_at:
type: string
format: date-time
readOnly: true
Единственные решения, которые я вижу:
- определение 2 определений (Message и UpdatedMessage) с использованием allOf
- не используя определение в методе PATCH или в методе GET/POST
Есть ли лучшее решение для достижения этого? Идеальным решением было бы иметь только одно определение Message и в методе PATCH переопределить определение Message (удалив поле).
Я не знаю, возможно ли это.
Спасибо