Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #13365
Closed
Open
Issue created Sep 06, 2022 by Administrator@rootContributor4 of 5 checklist items completed4/5 checklist items

[BUG][JAVA][SPRING] Required in body fields not being applied

Created by: sky-andremartins

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • [X Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using required in schema definition for a specific field it doesn't generate code to reflect that. Using the spec below it generates:

public class Person {

  @JsonProperty("name")
  private String name;

  @JsonProperty("id")
  private Integer id;

  public Person name(String name) {
    this.name = name;
    return this;
  }

  /**
   * Get name
   * @return name
  */
  @Schema(name = "name", required = true)
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Person id(Integer id) {
    this.id = id;
    return this;
  }

  /**
   * Get id
   * @return id
  */
  @Schema(name = "id", required = false)
  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Person person = (Person) o;
    return Objects.equals(this.name, person.name) &&
        Objects.equals(this.id, person.id);
  }

  @Override
  public int hashCode() {
    return Objects.hash(name, id);
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class Person {\n");
    sb.append("    name: ").append(toIndentedString(name)).append("\n");
    sb.append("    id: ").append(toIndentedString(id)).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    ");
  }
}

Which does not contain not null in the name field. When my controller receives that it is null if I didn't pass a name.

openapi-generator version

6.0.1

OpenAPI declaration file content or url
openapi: 3.0.0

paths:
  '/person':
    post:
      summary: Inserts a person
      operationId: postPerson
      tags:
        - person
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Person'

      responses: ...

components:
  schemas:
    Person:
      type: object
      required:
        - name
      properties:
        name:
          type: string
        id:
          type: integer
Generation Details
task openApiGenerateAppApi(type: GenerateTask) {
    generatorName = "spring"
    group = "api"
    inputSpec = "$rootDir/service/specs/api.yaml"
    packageName = "my.package"
    apiPackage = "my.package.api"
    modelPackage = "my.package.api.domain"
    outputDir = project(':service').buildDir.toString() + "/generated"
    configOptions = [
            library                          : "spring-boot",
            sourceFolder                     : "sources/java",
            interfaceOnly                    : "true",
            openApiNullable                  : "false",
            useBeanValidation                : "false",
            exceptionHandler                 : "false",
            sortModelPropertiesByRequiredFlag: "false",
            sortParamsByRequiredFlag         : "false",
            serializationLibrary             : "jackson",
            useTags                          : "true",
            enumPropertyNaming               : "PascalCase"
    ]
}
Steps to reproduce

Run task to generate and make a request to the endpoint without name field in the body.

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/13196

Suggest a fix
Assignee
Assign to
Time tracking