swagger-api / swagger-api/swagger-codegen
[JAVA] Expose "localVarPath" as a public static field, so it can be used by non-codegen clients
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
I have an application program which connects to a backend service via REST. The REST service exposes a description of its APIs via the Open API Specification. In the application code I use Swagger Codegen to generate client code to call the backend service. All works great :-)
However, there is one call where I need to do a few things that are not possible via the Swagger Codegen. (Capture arbitrary JSON that comes back from the service, pass additional parameters that are not described in the Open API Specification file, etc.). So for these I use my own HTTP client, and that works great too. :-)
My custom client has the URLs of the services it is calling hardcoded in the source. But actually this could come from Swagger Clientgen if the code generated were slightly differently.
Swagger-codegen version
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.3.1</version>
Suggest a fix/enhancement
The Swagger Codegen currently generates methods like:
public MyDto myApiCallUsingGET(...) throws ApiException {
...
String localVarPath = "/my-url/{param}"
.replaceAll("\\{" + "param" + "\\}", apiClient.escapeString(foo.toString()));
...
}
This could be changed, to expose via a public final static field, such as:
public static final String myApiCallUsingGETPathTemplate = "/my-url/{param}";
public MyDto myApiCallUsingGET(...) throws ApiException {
...
String localVarPath = myApiCallUsingGETPathTemplate
.replaceAll("\\{" + "param" + "\\}", apiClient.escapeString(foo.toString()));
...
}
This would have the advantage that my custom client code could access the URL path via this generated variable. If the server changed the URL, my code would continue to call the correct path. If the method got deleted from the server, my code would stop compiling. Clients which only use Swagger Codegen would not be affected.
(In my case, the method isn't actually using any path parameters, so it would be even more convenient, I could just take the URL and use it as is.)
Swagger is a great tool, but I guess all projects have a few things they have to do manually, and I could imagine this would be useful to all of them.
What do you think about this suggestion? I would be happy to attempt a PR if you are interested.
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 by locating the Java client template that generates the shown localVarPath variable and inspect how operation path templates are emitted. Expose each operation's path template as a public static final field, reuse it in the generated method, and verify that generated Java clients still compile and preserve the requested URL behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100