swagger-api / swagger-api/swagger-codegen

[JAXRS] Enable POST Server-side method to pass through @Context UriInfo uriInfo

Open
#5,632 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

Currently @Context securityContext is passed through to all method implementations which is fine. However, if I want to be a good citizen for POST operations and return a HTTP 201 created response, currently I cannot see a mechanism for retrieving the requesting URI information to be used in the Location Header field of the response.

Attempted workarounds:
Updating Mustache files
I have played with updating locally the following files:

  1. api.mustache
  2. apiService.mustache
  3. apiServiceImpl.mustache

For all references of SecurirtyContext securityContext also provide UriInfo uriInfo so that the method implementations have the necessary request information to hand. This works fine, but it could be argued that it pollutes all methods with an extra parameter where, at least for my current use-case, it will only be used in a POST method.

Manually adjust the code post code generation

An extra step that I could perform is to go into the code generated and update the 3 files (..Api.java, ..ApiService,java and ApiServiceImpl.java) to include the extra information I need. But with this you start to lose the benefit of code generation from a swagger definition file. Plus it will become a maintenance nightmare.

If there is another way to achieve my ultimate goal of creating a HTTP 201 response with original request URI details then I would appreciate that!

Swagger-codegen version

Currently using 2.2.1

Swagger declaration file content or url

Please see here https://gist.github.com/bsahlstedt/f5e699321e05b2f199bb44c894d3aece (swagger.json)

Command line used for generation

java -jar swagger-codegen-cli-2.2.1.jar generate -i swagger.json -l jaxrs --api-package "com.test.github" --model-package "com.test.github.model" --invoker-package "com.test.github.api" --group-id "com.test.github"

Steps to reproduce

The Code that is generated for the POST declaration /items/{id} is:

ItemsApiService.java
public abstract Response itemsIdPost(Long id,SecurityContext securityContext) throws NotFoundException;

ItemsApiServiceImpl.java

@Override
    public Response itemsIdPost(Long id, SecurityContext securityContext) throws NotFoundException {
        // do some magic!
        return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "magic!")).build();
    }

It is here that I want to perform a Response.created(uriInfo.getRequestUri()).build(); Or if better, another way of returning requesting URI information.

Related issues

Couldn't find any

Suggest a Fix

In a way the return Response.ok() 200 is not what the swagger definition was saying as only a return type 201 was set. So this could be conceived as a bug also!

  1. Update the 3 mustache files, affects all generated methods.
  2. Some generation logic to allow Request inform to passed through to POST - and potentially PUT and PATCH updates.

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 reading api.mustache, apiService.mustache, and apiServiceImpl.mustache, then inspect how the JAX-RS generator builds the POST declaration for /items/{id}. Compare the generated ItemsApiService.java and ItemsApiServiceImpl.java signatures with the requested UriInfo access, and verify that the generated implementation can create a 201 response using the request URI without requiring manual edits.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.