OpenAPITools / OpenAPITools/openapi-generator

How to pass HttpServletRequest to the API methods when using jersey2 in Java?

Open
#6,813 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

I am trying to figure out how to pass the HttpServletRequest request in the auto generated API methods.
I need the access to HttpServletRequest to get remote IP and remote requested URI. I might need it for other purpose also. Like getting the remote user principal or to get the logged in user.

openapi-generator version

Using gradle plugin version: 4.3.1
openapi: 3.0.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Preview Server API overview
  version: 1.0.0
  description: Preview Server Open API Specification

servers:
  - url: 'http://acme.com'

paths:
  /thumbnail/{uploadId}:
    get:
      operationId: upload
      tags:
        - Asset
        - Preview
      summary: Use this API to get the upload.
      parameters: 
        - $ref: '#/components/parameters/UploadId'
      responses:
        200:
          description: Returns the thumbnail of the given asset in JPEG format.
components:
  parameters:
    UploadId:
      name: uploadId
      in: path
      required: true
      description: ID of past upload
      schema:
        default: 1
        type: integer
Command line used for generation

Used gradle to generate it:

openApiGenerate {
  generatorName = "java"
  library = "jersey2"
  inputSpec = file("./src/api-spec/openapi-spec.yaml").path
  outputDir = "$buildDir/openapi-spec/codegen".toString()
  apiPackage = "com.acme.openapi.v3.api"
  invokerPackage = "com.acme.openapi.v3.api.invoker"
  modelPackage = "com.acme.openapi.v3.api.model"

  configOptions = [
    dateLibrary: "java8-localdatetime"
  ]
  systemProperties = [
    modelDocs: "false"
  ]
  skipValidateSpec = true
  logToStderr = true
  generateAliasAsModel = false
  // set to true and set environment variable {LANG}_POST_PROCESS_FILE
  // (e.g. SCALA_POST_PROCESS_FILE) to the linter/formatter to be processed.
  // This command will be passed one file at a time for most supported post processors.
  enablePostProcessFile = false
}

Generated Output
  @io.swagger.annotations.ApiOperation(value = "Use this API to get the upload.", notes = "", response = Void.class, tags = {"Upload"})
  @io.swagger.annotations.ApiResponses(value = {
    @io.swagger.annotations.ApiResponse(code = 200, message = "Use this API to get the upload.", response = Void.class)})
  public Response upload(@ApiParam(value = "ID of past upload", required = true, defaultValue = "1") @PathParam("uploadId") @NotNull Integer uploadId,
@Context SecurityContext securityContext, 
@Context HttpServletRequest request)
    throws NotFoundException {
    return delegate.upload(uploadId, securityContext);
  }
I want

I want to generate the code in such a way that HttpServletRequest request is passed in my API methods.
I did lots of googling and still not able to find the fix.
I want to how can I access the HttpServletRequest request in the requested API calls.

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 Java jersey2 generator configuration in the Gradle block and compare the generated API method with the delegate call shown in the output. Check how the generator handles HttpServletRequest and whether this is documented or configurable; done means a reproducible way to access it in generated API calls or a clearly scoped generator change.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.