Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

[Spring]: RequestBody required-handling

Đang mở
#1,432 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
62/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
java, openapi, spring
Lĩnh vực
backend-api-design

Hướng nghiên cứu

The bug is that the spring generator ignores requestBody.required: false and always emits @RequestBody with its default required=true. Start in the Spring server templates and find where the @RequestBody annotation is written for request bodies. Make it pass required from the spec, then check that a POST to /search with no body and no query parameter is accepted. Done when the generated controller compiles and both curl cases from the issue succeed.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Bug

Description

My goal is to have an endpoint to enable a quick search and an extended search. Using the quick search the endpoint only needs a query-param. Using the extended search a full search-body is required.

I was defining my openapi as follows:

/search:
    post:
      requestBody:
        required: false
        content:
          application/json:
            schema:
              type: array
              items:
                $ref: "#/components/schemas/SearchCriterion"
      parameters:
        - in: query
          name: value
          required: false
          schema:
            type: string
      responses:
        "200":
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/SearchResult"

I was expecting to be able to send curl requests like:

  • curl -X POST
    'http://localhost/search'
    -H 'Content-Type: application/json'
    -d '[{"type":"NAME","value":"Dummy"}]'
  • curl -i -X POST
    'http://localhost/search?value=something'
    -H 'Content-Type: application/json'

But only the first request returned any data. The second request only resulted in a 404.

After shortly analysing the problem I found that the required-property is not handed to the @RequestBody Spring annotation. As the annotation uses true as default for the required attribute, I need to hand the body for every request.

This behaviour should be altered to correctly pass the required-property to the code.

Version

We are using [email protected] (which uses [email protected]) but the current main is also not passing the required-property.

Language / Generator Affected

  • spring (i haven't checked any other generators as this should be specific for this generator)

OpenAPI/Swagger Spec

/search:
    post:
      requestBody:
        required: false
        content:
          application/json:
            schema:
              type: array
              items:
                $ref: "#/components/schemas/SearchCriterion"
      parameters:
        - in: query
          name: value
          required: false
          schema:
            type: string
      responses:
        "200":
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/SearchResult"

Environment

OS: Windows 11

Java Version: temurin-21.0.6+7.0.LTS

Build Tool: Maven

swagger-codegen-generators Version: 1.0.54

Checklist

  • [/] I have searched the existing issues to avoid duplicates.
  • [/] I have included a minimal reproducible spec or configuration.
  • [/] I have clearly described the steps to reproduce the issue.
  • [/] I have specified which generator or template is affected.
Ngôn ngữ chính
Mustache
Star
299
Fork
439
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của swagger-api/swagger-codegen-generators

Tất cả issue của swagger-api/swagger-codegen-generators

Issue tương tự

Thêm issue về Backend & API Design

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.