[REQ] Generate Spring HTTP Interface with configurable URL placeholder via Spring properties

Open Beginner friendly
#19,120 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Stale
Tech stack
java, spring
Domain
api, backend

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.

Description

Enhancement: Feature

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.

Dominant language
Java
Stars
26.8k
Forks
7.7k
Avg merge
1d 10h
Merged PRs (30d)
103

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.

More from OpenAPITools/openapi-generator

All issues in OpenAPITools/openapi-generator

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.