Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(JAVA SPRING RESTCLIENT)Fix missing imports when using withXml for Spring RestClient and fix its README #19146

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package {{invokerPackage}};

{{#withXml}}
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
{{/withXml}}
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
Expand All @@ -12,8 +16,8 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
{{#withXml}}
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
{{/withXml}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@
Building the API client library requires:

1. Java 17+
{{#jersey2}}
2. Maven (3.8.3+)/Gradle (7.2+)
{{/jersey2}}
{{^jersey2}}
2. Maven/Gradle
{{/jersey2}}

## Installation

Expand Down Expand Up @@ -87,33 +82,6 @@ Then manually install the following JARs:
- `target/{{{artifactId}}}-{{{artifactVersion}}}.jar`
- `target/lib/*.jar`

{{#jersey2}}
## Usage

To add a HTTP proxy for the API client, use `ClientConfig`:
```java
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import {{{invokerPackage}}}.*;
import {{{package}}}.{{{classname}}};

...

ApiClient defaultClient = Configuration.getDefaultApiClient();
ClientConfig clientConfig = defaultClient.getClientConfig();
clientConfig.connectorProvider(new ApacheConnectorProvider());
clientConfig.property(ClientProperties.PROXY_URI, "http://proxy_url_here");
clientConfig.property(ClientProperties.PROXY_USERNAME, "proxy_username");
clientConfig.property(ClientProperties.PROXY_PASSWORD, "proxy_password");
defaultClient.setClientConfig(clientConfig);

{{{classname}}} apiInstance = new {{{classname}}}(defaultClient);
{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}}
```

{{/jersey2}}
## Getting Started

Please follow the [installation](#installation) instruction and execute the following Java code:
Expand All @@ -128,7 +96,7 @@ import {{{package}}}.{{{classname}}};
public class {{{classname}}}Example {

public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
ApiClient defaultClient = new ApiClient();
defaultClient.setBasePath("{{{basePath}}}");
{{#hasAuthMethods}}{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
// Configure HTTP basic authorization: {{{name}}}
Expand Down Expand Up @@ -173,10 +141,10 @@ public class {{{classname}}}Example {
try {
{{#returnType}}{{{.}}} result = {{/returnType}}apiInstance.{{{operationId}}}({{#allParams}}{{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}});{{#returnType}}
System.out.println(result);{{/returnType}}
} catch (ApiException e) {
} catch (HttpStatusCodeException e) {
System.err.println("Exception when calling {{{classname}}}#{{{operationId}}}");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Status code: " + e.getStatusCode().value());
System.err.println("Reason: " + e.getResponseBodyAsString());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2955,4 +2955,26 @@ public void shouldGenerateOAuthTokenSuppliers() {

}

@Test public void testRestClientWithXML_issue_19137() {
final Path output = newTempFolder();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("java")
.setLibrary(JavaClientCodegen.RESTCLIENT)
.setAdditionalProperties(Map.of(
CodegenConstants.API_PACKAGE, "xyz.abcdef.api",
CodegenConstants.WITH_XML, true
))
.setInputSpec("src/test/resources/3_1/java/petstore.yaml")
.setOutputDir(output.toString().replace("\\", "/"));

List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();

validateJavaSourceFiles(files);
TestUtils.assertFileContains(
output.resolve("src/main/java/xyz/abcdef/ApiClient.java"),
"import com.fasterxml.jackson.dataformat.xml.XmlMapper;",
"import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;"
);
}

}
8 changes: 4 additions & 4 deletions samples/client/echo_api/java/restclient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import org.openapitools.client.api.AuthApi;
public class AuthApiExample {

public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
ApiClient defaultClient = new ApiClient();
defaultClient.setBasePath("http://localhost:3000");

// Configure HTTP basic authorization: http_auth
Expand All @@ -101,10 +101,10 @@ public class AuthApiExample {
try {
String result = apiInstance.testAuthHttpBasic();
System.out.println(result);
} catch (ApiException e) {
} catch (HttpStatusCodeException e) {
System.err.println("Exception when calling AuthApi#testAuthHttpBasic");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Status code: " + e.getStatusCode().value());
System.err.println("Reason: " + e.getResponseBodyAsString());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ import org.openapitools.client.api.ResourceApi;
public class ResourceApiExample {

public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
ApiClient defaultClient = new ApiClient();
defaultClient.setBasePath("http://localhost");

ResourceApi apiInstance = new ResourceApi(defaultClient);
try {
File result = apiInstance.resourceInResponse();
System.out.println(result);
} catch (ApiException e) {
} catch (HttpStatusCodeException e) {
System.err.println("Exception when calling ResourceApi#resourceInResponse");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Status code: " + e.getStatusCode().value());
System.err.println("Reason: " + e.getResponseBodyAsString());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ import org.openapitools.client.api.DefaultApi;
public class DefaultApiExample {

public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
ApiClient defaultClient = new ApiClient();
defaultClient.setBasePath("http://localhost");

DefaultApi apiInstance = new DefaultApi(defaultClient);
try {
List<ByteArrayObject> result = apiInstance.nullableArrayTestGet();
System.out.println(result);
} catch (ApiException e) {
} catch (HttpStatusCodeException e) {
System.err.println("Exception when calling DefaultApi#nullableArrayTestGet");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Status code: " + e.getStatusCode().value());
System.err.println("Reason: " + e.getResponseBodyAsString());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
Expand Down
8 changes: 4 additions & 4 deletions samples/client/petstore/java/restclient-swagger2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,18 @@ import org.openapitools.client.api.AnotherFakeApi;
public class AnotherFakeApiExample {

public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
ApiClient defaultClient = new ApiClient();
defaultClient.setBasePath("http://petstore.swagger.io:80/v2");

AnotherFakeApi apiInstance = new AnotherFakeApi(defaultClient);
Client client = new Client(); // Client | client model
try {
Client result = apiInstance.call123testSpecialTags(client);
System.out.println(result);
} catch (ApiException e) {
} catch (HttpStatusCodeException e) {
System.err.println("Exception when calling AnotherFakeApi#call123testSpecialTags");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Status code: " + e.getStatusCode().value());
System.err.println("Reason: " + e.getResponseBodyAsString());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
Expand Down
8 changes: 4 additions & 4 deletions samples/client/petstore/java/restclient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,18 @@ import org.openapitools.client.api.AnotherFakeApi;
public class AnotherFakeApiExample {

public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
ApiClient defaultClient = new ApiClient();
defaultClient.setBasePath("http://petstore.swagger.io:80/v2");

AnotherFakeApi apiInstance = new AnotherFakeApi(defaultClient);
Client client = new Client(); // Client | client model
try {
Client result = apiInstance.call123testSpecialTags(client);
System.out.println(result);
} catch (ApiException e) {
} catch (HttpStatusCodeException e) {
System.err.println("Exception when calling AnotherFakeApi#call123testSpecialTags");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Status code: " + e.getStatusCode().value());
System.err.println("Reason: " + e.getResponseBodyAsString());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
Expand Down
Loading