swagger-api / swagger-api/swagger-codegen-generators

JavaSpring codegen ignores per-field required setting

Open
#1,310 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
299
Forks
439
PR merge metrics
No merged PRs in 30d

Description

Given the below OAS 3 spec

paths:
  /survey:
    post:
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
              properties:
                name:
                  type: string
                age: 
                  type: integer
              required:
                - name

The expectation is to require a non-null request body but only name to be mandatory. The age property should become optional. However, when code is generated it produces:

    @RequestMapping(value = "/survey",
        consumes = { "application/x-www-form-urlencoded" },
        method = RequestMethod.POST)
    default ResponseEntity<Void> operation(
        @Parameter(in = ParameterIn.DEFAULT, description = "",schema=@Schema()) @RequestParam(value="name", required=true)  String name, 
        @Parameter(in = ParameterIn.DEFAULT, description = "",schema=@Schema()) @RequestParam(value="age", required=true)  Integer age) {

Observe that age parameter in the generated method signature has required=true when it's clearly not required in the spec.

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 by reproducing the supplied OAS 3 application/x-www-form-urlencoded case in the JavaSpring generator and compare the generated method signature with the schema's required list. Done means the generated signature marks name as required, age as optional, and keeps the request body required.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.