Swagger request body required validation not working

4.2k views Asked by At

I am having my project created using swagger "2.0". Problem is that I want to have some mandatory fields in request body and needs to be validated. Swagger is not able to validate the fields inside the body (Schema Object) and I would need it to throw 400 Bad request before reaching to the actual API implementation.

Here's the actual code I wrote in my yaml file.

'/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**

## Model generated in Java Code by Swagger:

@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 ");
}
}

I tried both the ways (as bolded) by setting required as true & by adding property "body" under the required: but it's not working. Only in swagger UI it's showing the error. But when I try to hit the API directly from external (like Postman), it's allowing me to hit the API without the body(String) as expected in AddMessageInput model. I had to explicitly null check on this field. Please suggest me if I am missing anything.

TIA for your kind responses.

0

There are 0 answers