我正在使用 swagger “2.0” 创建我的项目。问题是我想在请求正文中有一些必填字段并且需要进行验证。Swagger 无法验证正文中的字段(架构对象),我需要它在到达实际 API 实现之前抛出 400 Bad request。
这是我在 yaml 文件中编写的实际代码。
'/groupChats/{pathparam}/message':
post:
tags:
- group chats
description: Adds a message to the given group chat.
operationId: addMessageToGroupChat
consumes:
- application/json
parameters:
- name: pathparam
description: some pathparam
in: path
required: true
type: string
- name: addMessageInput
description: The content of the message to add to the groupChat.
in: body
required: true
schema:
$ref: '#/definitions/AddMessageInput'
- name: Authorization
in: header
type: string
required: false
description: >-
The identification token. It can be a JSON web token, a Basic
Authorization token, etc.
- name: mode
in: header
type: string
required: false
description: >-
there is a high performance mode that tries and optimize the messages
responses:
'201':
description: Message added.
schema:
$ref: '#/definitions/AddMessageOutput'
AddMessageInput:
type: object
properties:
body:
description: The message body.
type: string
**required: true**
tags:
description: some desc
type: array
items:
$ref: '#/definitions/MessageTag'
**required:
- body**
## Swagger 在 Java 代码中生成的模型:
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.CompanyHealthSpringCodegen", date = "2017-08-31T20:00:34.574+05:30")
public class AddMessageInput implements PersistenceInformationBearer {
private String body = null;
private List tags = new ArrayList();
@jsonignore
private boolean isNew = false;
/**
The message body.
**/
public AddMessageInput body(String body) {
this.body = body;
return this;
}
@apimodelproperty(required = true, value = "The message body.")
@jsonproperty("body")
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
/**
Additional tags to be added to the message. These tags will be not encrypted. If you add two tags with the same tag name, the behavior is undefined.
**/
public AddMessageInput tags(List tags) {
this.tags = tags;
return this;
}
@apimodelproperty(value = "Additional tags to be added to the message. These tags will be not encrypted. If you add two tags with the same tag name, the behavior is undefined.")
@jsonproperty("tags")
public List getTags() {
return tags;
}
public void setTags(List tags) {
this.tags = tags;
}
/**
This method returns:
true if the object instance refers to a newly created messaging object (e.g. newly created group chat).
false if the object instance refers to an existing messaging object (e.g. returning a group chat that
already exists).
@return
*/
@override
@jsonignore
public boolean isNew() {
return this.isNew;
}
/**
Sets the isNew attribute, which describes if the object the
instance referring to (e.g. groupChat) is new or already exists
*/
@jsonignore
public void setIsNew(boolean isNew) {
this.isNew = isNew;
}
@override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AddMessageInput addMessageInput = (AddMessageInput) o;
return Objects.equals(body, addMessageInput.body) &&
Objects.equals(tags, addMessageInput.tags);
}
@override
public int hashCode() {
return Objects.hash(body, tags);
}
@override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class AddMessageInput {\n");
sb.append(" body: ").append(toIndentedString(body)).append("\n");
sb.append(" tags: ").append(toIndentedString(tags)).append("\n");
sb.append("}");
return sb.toString();
}
/**
Convert the given object to string with each line indented by 4 spaces
(except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
我通过将required 设置为 true并在required:下添加属性“body”尝试了两种方式(如粗体所示),但它不起作用。只有在 swagger UI 中才会显示错误。但是当我尝试直接从外部(如 Postman)访问 API 时,它允许我在没有正文(字符串)的情况下访问 API,正如 AddMessageInput 模型中所预期的那样。我必须明确地对这个字段进行空检查。如果我缺少任何东西,请建议我。
TIA 为您的友好回应。