swagger-api / swagger-api/swagger-codegen

[SPRING] Validation not working for Request Params

Open
#9,772 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Validation is not working for Request Params with SPRING GENERATED SERVER STUBS. GET request for a invalid Query Param is successful.

Swagger-codegen version

Swagger Code Generator 3.0.11

Swagger declaration file content or url

Below are some of the Query Params in YAML file -

parameters:
offset: # <-- Arbitrary name for the definition that will be used to refer to it.
# Not necessarily the same as the parameter name.
in: query
name: offset
required: false
schema:
type: integer
minimum: 0
default: 0
description: The number of items to skip before starting to collect the result set.
limit:
in: query
name: limit
required: false
schema:
type: integer
minimum: 1
maximum: 10000
default: 200 #This will be configured in System-config properties file
description: The numbers of items to return.

Generated controller
public ResponseEntity<List> retrieveCustomObjects(@Min(0)@ApiParam(value = "The number of items to skip before starting to collect the result set.", allowableValues = "") @Valid @RequestParam(value = "offset", required = false) Integer offset,@Min(1) @Max(10000) @ApiParam(value = "The numbers of items to return.", allowableValues = "") @Valid @RequestParam(value = "limit", required = false) Integer limit,@ApiParam(value = "Fields to be present in response.") @Valid @RequestParam(value = "fields", required = false) String fields,@ApiParam(value = "Sorting fields.") @Valid @RequestParam(value = "sort", required = false) String sort,@Min(0) @Max(10) @ApiParam(value = "Depth of the expansion", allowableValues = "") @Valid @RequestParam(value = "depth", required = false) Integer depth,@ApiParam(value = "References to be expanded") @Valid @RequestParam(value = "expand", required = false) String expand) {
return delegate.retrieveCustomObjects(offset, limit, fields, sort, depth, expand);
}

Suggest a fix/enhancement

Include @Validation in apiController.mustache file at the class level . Request Params are validated with this annotation.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the apiController.mustache template and compare it with the generated controller shown in the issue. Check how class-level validation is represented in Spring server stubs and verify that invalid offset, limit, and depth query values are rejected. Done means the generated controller includes the requested validation annotation and enforces the declared bounds.

Written by the indexing model from the issue text.

Assessment

Tech stack
spring
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.