swagger-api / swagger-api/swagger-codegen

[JAVA] Support Javax injection for creation of API delegate instead of static factory pattern

Open
#3,930 16 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature General: Suggestion help wanted
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Replace/Complement static factory with DI injection

Currently, the generator for jersey and rest-easy use the static factory pattern to create the delegate that gets called by the REST API.

Swagger-codegen version

Whatever version this gradle plugin in using, (I think it's the current release).

Swagger declaration file content or url

Use the pet-store as an example.

Command line used for generation
swagger {
    inputSpec = "${project.projectDir.path}/spec/swagger.yaml"

    output = 'build/swagger'
    language = 'jaxrs-resteasy'

    additionalProperties = [
            'invokerPackage'   : 'edu.wpi.grip.web.swagger',
            'modelPackage'     : 'edu.wpi.grip.web.swagger.model',
            'apiPackage'       : 'edu.wpi.grip.web.swagger.api',
            'serializableModel': 'true'
    ]

    apis = ''
    models = ''
    supportingFiles = ''
}
Suggest a Fix

I'm using a custom api.mustache file that replaces the delegate creation with constructor injection.

import javax.inject.Inject;

public class {{classname}}  {
    private final {{classname}}Service delegate;

    @Inject
    {{classname}}({{classname}}Service delegate) {
        this.delegate = delegate;
    }

If a zero argument constructor is needed for other reasons and other frameworks both solutions could be implemented in parallel.

import javax.inject.Inject;

public class {{classname}}  {
    private final {{classname}}Service delegate;

    {{classname}}() { // Zero argument constructor because some frameworks need this
        this({{classname}}ServiceFactory.get{{classname}}(););
    }

    @Inject
    {{classname}}({{classname}}Service delegate) {
        this.delegate = delegate;
    }

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 the jersey and rest-easy API generation templates, including the custom api.mustache example, and generate the pet-store API with the provided Gradle configuration. Compare the generated delegate construction with the requested javax.inject constructor approach and verify whether the zero-argument factory path must remain available for other frameworks.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design, tooling
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.