OpenAPITools / OpenAPITools/openapi-generator
[REQ] Generate Spring HTTP Interface with configurable URL placeholder via Spring properties
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
This feature request proposes an enhancement to the OpenAPI Generator Java spring-http-interface. It aims to introduce the ability to generate interfaces annotated with @HttpExchange (Spring Framework 3) where the field url or value attribute can be configured using a placeholder with a default value (the path defined in the API specification).
This is a snippet of the current mustache template:
@HttpExchange(
method = "{{{httpMethod}}}",
value = "{{{path}}}",
accept = { {{#vendorExtensions.x-accepts}}"{{.}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-accepts}} }{{#vendorExtensions.x-content-type}},
contentType = "{{{vendorExtensions.x-content-type}}}"{{/vendorExtensions.x-content-type}}
)
Proposed Implementation
Update the mustache template to add a placeholder with a default value, for example:
@HttpExchange(
method = "{{{httpMethod}}}",
value = "${openapi.{{operationId}}.base-path:{{path}}}",
accept = { {{#vendorExtensions.x-accepts}}"{{.}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-accepts}} }{{#vendorExtensions.x-content-type}},
contentType = "{{{vendorExtensions.x-content-type}}}"{{/vendorExtensions.x-content-type}}
)
Creating a custom version of this template embedded into my application is not the best solution.
Does anyone know a way to achieve what I'm asking for using the current template?
Example
Snippet of API definition example.yaml
paths:
/users:
get:
summary: Get all users
operationId: getUsers
/products/{productId}:
get:
summary: Get a product by ID
operationId: getProductById
Generated ExampleService.java
public interface ExampleService {
@HttpExchange(
method = "GET",
value = "${openapi.getUsers.base-path:/users}",
accept = { "application/json" }
)
ResponseEntity<List<User>> getUsers();
@HttpExchange(
method = "GET",
value = "${openapi.getProductById.base-path:/products/{productId}",
accept = { "application/json" }
)
ResponseEntity<Product> getProductById(@PathVariable String productId);
}
Snippet of application.properties file
openapi.getUsers.base-path=/new/path/to/users
openapi.getProductById.base-path=/new/path/to/products/{productId}
Benefis
- Flexibility: this feature allows for dynamic configuration of API paths, potentially from environment variables or configuration files
- Maintainability: separating the path definition from the annotation improves code maintainability and reduces duplication
I hope the OpenAPI Generator maintainers find this feature useful. I would be happy to proceed with implementation. Thank you.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-http-interface/api.mustache and compare it with the example.yaml and generated ExampleService.java shown in the issue. Verify the generated @HttpExchange values support configurable Spring property placeholders while retaining the specification paths as defaults, including paths with variables such as /products/{productId}.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100