swagger-api / swagger-api/swagger-codegen

[JaxRS] Enable Context Injection

Open
#8,321 3 comments 7 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 don't see any options/possibilities to inject the various @Context types of the jax-rs specification into the generated Operations/Methods. In some cases this is crucial, since you may want to access headers, servlet, url information or more. It is not possible to add/override this information in an implementation of the interface api since it belongs to the method signature.

Post generation edit is obviously a workaround, but the advantage of the generated interface would be lost and the api becomes difficult to maintain. So there must be a way to generate it from the open api / swagger specification.

Related issues/PRs

#5632 #1588

Suggest a fix/enhancement

Since you don't want to have all available contexts in a generated operation, i suggest that this becomes configurable.

I'd probably solve this like that:
Supply a map of Contexts and OperationIds to the generator with a property

So given the following option:

 contextInjection=[getPets=[UrlInformation, HTTPHeaders], addNewPet=[UrlInformation, SecurityContext]

I'd like to have this result:

    @Path("pets")
    public interface PetApi {
        @GET
        Response getPets(@Context UrlInformation urlInfo, @Context HttpHeaders http); 
        
        @POST
         Response addPet(Pet pet, @Context UrlInformation urlInfo, @Context SecurityContext securityContext); 
    }

Maybe there is also an option to add this to the open api / swagger spec as a custom extension, but this would make the api "platform aware".

Any thoughts?

If nobody comes up with a better solution I'll try to implement this in a few days since I need it for a project.

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 tracing how the generator produces JAX-RS interfaces and how options are supplied for generated operations. Compare the requested per-operation context mapping with the existing OpenAPI/Swagger inputs, then define a configurable approach and verify that generated methods receive the selected @Context parameters without adding unwanted contexts.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.