swagger-api / swagger-api/swagger-codegen

JAX-RS server stubs from Swagger 2 APIs don't handle 'body' parameters properly?

Open
#9,404 1 comment 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

I have a (mandatory) 'body' parameter in an API call that's not reliably deserialized. If I use 'Postman' to construct a test query the whole query comes in a single packet and the API works as expected. If, however, I use the generated 'cpprest' code the request is sent in two package (first packages has headers; second packet has body). Jetty, apparently by design, dispatches the call as soon as the headers have been received. When using my 'cpprest' client, however, ends up invoking my implementation with a "null" parameter?

Swagger-codegen version

I'm using SwaggerHub's code gen option for a Swagger 2 API (not yet OpenAPI 3.0).

Swagger declaration file content or url

The API in question is spec'ed as follows:

...
paths:
  /hosts:
    put:
      description: Add a host
      tags:
        - Hosts
      operationId: AddHost
      consumes:
        - application/json
        - application/xml
      produces:
        - application/json
        - application/xml
      parameters:
        - name: HostSpec
          description: Specification of new host
          in: body
          schema:
            $ref: '#/definitions/HostSpec'
          required: true
...
Command line used for generation
Steps to reproduce

The code generated by SwaggerHub has the following ‘xxxImpl’ class code:

...
public class HostsApiServiceImpl extends HostsApiService {
...
@Override
public Response addHost(HostSpec hs, SecurityContext securityContext) throws NotFoundException {
...

and, sure enough, if the header and body are sent together, the ‘hs’ parameter refers to a properly formatted HostSpec with the fields I passed from the client stubs. When the body is not yet present, however, that parameter is null.

Related issues/PRs
Suggest a fix/enhancement

Should I be explicitly checking for “null” body parameters and manually waiting to receive + deserialize the ‘body’ parameter? Does my implementation code need to be aware of the timing of receipt of the API call's 'body'?

If there's any complete sample code (which actually collects the 'body' parameter and not something that just blindly responds with "Magic!") I'd really appreciate a pointer. Thanks!

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

Use the Swagger 2 declaration for PUT /hosts and the generated HostsApiServiceImpl.addHost entry point as starting points. Compare requests sent by Postman and cpprest, then verify that a required HostSpec body reaches addHost consistently rather than as null.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.