OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA/RestTemplate] Only the path is made available for metrics collectors when expanding URI variables

Open
#10,810 1 comment 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When URI variables are used then only the part of the URI defined in paths is expanded in the template apiClient.mustache, leading to metric collectors like MetricsClientHttpRequestInterceptor not getting the absolute URI. When expansion is done by RestTemplate an absolute URI is expected, and therefore an absolute URI is also expected when the expansion is done by the generated API client.

For the example in the file below /users/{userId} is expanded (and stored for metrics), instead of the absolute URI https://www.example.com/api/v1/users/{userId}, in other words MetricsClientHttpRequestInterceptor receives only the /users/{userId} part.

Actual value: /users/{userId}
Expected value: https://www.example.com/api/v1/users/{userId}

openapi-generator version

openapi-generator-cli 5.3.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: 1.0.0
  title: Sample API
servers:
  - url: https://www.example.com/api/v1
paths:
  /users/{userId}:
    get:
      summary: Get a user by ID
      parameters:
        - in: path
          name: userId
          schema:
            type: integer
          required: true
      responses:
        '200':
          description: Successful response
Generation Details
$ openapi-generator-cli.sh generate -g java --library resttemplate -i test.yaml 
Steps to reproduce
Related issues/PRs

This PR expanded the path templates via resttemplate's uriTemplateHandler, but did not use the complete URI:

https://github.com/OpenAPITools/openapi-generator/pull/3500

Suggest a fix/enhancement

Currently only the path is expanded in the apiClient.mustache file for JAVA/RestTemplate as seen on line 693-694 (https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache#L693):

String expandedPath = this.expandPath(finalUri, uriParams);
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(expandedPath); 

If the expansion of the absolute URI is done instead, then the metrics collectors gets the absolute URI template:

String expandedPath = this.expandPath(basePath + finalUri, uriParams);
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(expandedPath); 

The concatenation of the basePath and the finalUri should be ok since the paths have to be prefixed with / for the openapi document to be valid.

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 in modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache around lines 693-694, and review the Java RestTemplate generation invoked with openapi-generator-cli.sh. Reproduce the sample OpenAPI definition and verify that metrics collectors receive the absolute URI template, including the server base path, rather than only /users/{userId}.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.