OpenAPITools / OpenAPITools/openapi-generator

[BUG] go-server and go-gin-server missing required field in generated source code

Open
#10,429 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

When defining a requestBody, the models for the generated server stubs do not enforce the required attribute for properties. When I tried it out, it didn't work for the go-server and go-gin-server generators.

ie:

...
requestBody:
  required: true
  content:
    application/json:
      schema:
        title: createAccountRequestBody
        type: "object"
        properties:
          username:
            description: "The user's username"
            type: string
          email:
            description: "The user's email"
            type: string
          password:
            description: "The user's password"
            type: string
        required:
          - username
          - email
          - password
...

I took a look at the model.mustache file and it looks like the properties field are missing the binding:"required" within the structs.

The line in the go-gin-server model.mustache file:

25: ... `json:"{{baseName}}{{^required}},omitempty{{/required}}" ...

What I think the expected line should be:

25: ... `json:"{{baseName}}{{^required}},omitempty"{{/required}}{{required}} binding:"required"{{/required}}" ...

I'm not too familiar with Go, but failure expected? I would think we would want a more strict validator. If this is not expected behavior, I can try to help out and open up a PR.

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 go-gin-server model.mustache file linked in the issue and compare it with the corresponding go-server template. Reproduce the example requestBody specification, then inspect the generated model structs. Done means required requestBody properties are represented consistently in both generators and their validation behavior is verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.