Skip to content

Commit f93e75b

Browse files
saigiridhar21wing328
authored andcommitted
[java-okhttp]: Make example snippets code compilable (#3016)
* fix(okhttp-gson): Make example executable * fix(okhttp-gson): Make example executable * fix(okhttp-gson): Make example executable * fix(okhttp-gson): Updating pet project * fix(okhttp-gson): Updating parcelable project * fix(okhttp-gson): Using {{{}}}
1 parent 0a037ae commit f93e75b

File tree

14 files changed

+1979
-1174
lines changed

14 files changed

+1979
-1174
lines changed

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/README.mustache

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -77,48 +77,52 @@ Please follow the [installation](#installation) instruction and execute the foll
7777

7878
```java
7979
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}
80-
import {{{invokerPackage}}}.*;
81-
import {{{invokerPackage}}}.auth.*;
82-
import {{{modelPackage}}}.*;
80+
// Import classes:
81+
import {{{invokerPackage}}}.ApiClient;
82+
import {{{invokerPackage}}}.ApiException;
83+
import {{{invokerPackage}}}.Configuration;{{#hasAuthMethods}}
84+
import {{{invokerPackage}}}.auth.*;{{/hasAuthMethods}}
85+
import {{{invokerPackage}}}.models.*;
8386
import {{{package}}}.{{{classname}}};
8487

85-
import java.io.File;
86-
import java.util.*;
87-
88-
public class {{{classname}}}Example {
89-
90-
public static void main(String[] args) {
91-
{{#hasAuthMethods}}ApiClient defaultClient = Configuration.getDefaultApiClient();
92-
{{#authMethods}}{{#isBasic}}
93-
// Configure HTTP basic authorization: {{{name}}}
94-
HttpBasicAuth {{{name}}} = (HttpBasicAuth) defaultClient.getAuthentication("{{{name}}}");
95-
{{{name}}}.setUsername("YOUR USERNAME");
96-
{{{name}}}.setPassword("YOUR PASSWORD");{{/isBasic}}{{#isApiKey}}
97-
// Configure API key authorization: {{{name}}}
98-
ApiKeyAuth {{{name}}} = (ApiKeyAuth) defaultClient.getAuthentication("{{{name}}}");
99-
{{{name}}}.setApiKey("YOUR API KEY");
100-
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
101-
//{{{name}}}.setApiKeyPrefix("Token");{{/isApiKey}}{{#isOAuth}}
102-
// Configure OAuth2 access token for authorization: {{{name}}}
103-
OAuth {{{name}}} = (OAuth) defaultClient.getAuthentication("{{{name}}}");
104-
{{{name}}}.setAccessToken("YOUR ACCESS TOKEN");{{/isOAuth}}
105-
{{/authMethods}}
106-
{{/hasAuthMethods}}
107-
108-
{{{classname}}} apiInstance = new {{{classname}}}();
109-
{{#allParams}}
110-
{{{dataType}}} {{{paramName}}} = {{{example}}}; // {{{dataType}}} | {{{description}}}
111-
{{/allParams}}
112-
try {
113-
{{#returnType}}{{{returnType}}} result = {{/returnType}}apiInstance.{{{operationId}}}{{^vendorExtensions.x-group-parameters}}({{#allParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}({{#requiredParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/requiredParams}}){{#optionalParams}}
114-
.{{{paramName}}}({{{paramName}}}){{/optionalParams}}
115-
.execute();{{/vendorExtensions.x-group-parameters}}{{#returnType}}
116-
System.out.println(result);{{/returnType}}
117-
} catch (ApiException e) {
118-
System.err.println("Exception when calling {{{classname}}}#{{{operationId}}}");
119-
e.printStackTrace();
120-
}
88+
public class Example {
89+
public static void main(String[] args) {
90+
ApiClient defaultClient = Configuration.getDefaultApiClient();
91+
defaultClient.setBasePath("{{{basePath}}}");
92+
{{#hasAuthMethods}}
93+
{{#authMethods}}{{#isBasic}}
94+
// Configure HTTP basic authorization: {{{name}}}
95+
HttpBasicAuth {{{name}}} = (HttpBasicAuth) defaultClient.getAuthentication("{{{name}}}");
96+
{{{name}}}.setUsername("YOUR USERNAME");
97+
{{{name}}}.setPassword("YOUR PASSWORD");{{/isBasic}}{{#isApiKey}}
98+
// Configure API key authorization: {{{name}}}
99+
ApiKeyAuth {{{name}}} = (ApiKeyAuth) defaultClient.getAuthentication("{{{name}}}");
100+
{{{name}}}.setApiKey("YOUR API KEY");
101+
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
102+
//{{{name}}}.setApiKeyPrefix("Token");{{/isApiKey}}{{#isOAuth}}
103+
// Configure OAuth2 access token for authorization: {{{name}}}
104+
OAuth {{{name}}} = (OAuth) defaultClient.getAuthentication("{{{name}}}");
105+
{{{name}}}.setAccessToken("YOUR ACCESS TOKEN");{{/isOAuth}}
106+
{{/authMethods}}
107+
{{/hasAuthMethods}}
108+
109+
{{{classname}}} apiInstance = new {{{classname}}}(defaultClient);
110+
{{#allParams}}
111+
{{{dataType}}} {{{paramName}}} = {{{example}}}; // {{{dataType}}} | {{{description}}}
112+
{{/allParams}}
113+
try {
114+
{{#returnType}}{{{returnType}}} result = {{/returnType}}apiInstance.{{{operationId}}}{{^vendorExtensions.x-group-parameters}}({{#allParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}({{#requiredParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/requiredParams}}){{#optionalParams}}
115+
.{{{paramName}}}({{{paramName}}}){{/optionalParams}}
116+
.execute();{{/vendorExtensions.x-group-parameters}}{{#returnType}}
117+
System.out.println(result);{{/returnType}}
118+
} catch (ApiException e) {
119+
System.err.println("Exception when calling {{{classname}}}#{{{operationId}}}");
120+
System.err.println("Status code: " + e.getCode());
121+
System.err.println("Reason: " + e.getResponseBody());
122+
System.err.println("Response headers: " + e.getResponseHeaders());
123+
e.printStackTrace();
121124
}
125+
}
122126
}
123127
{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}}
124128
```

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/api_doc.mustache

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,52 @@ Method | HTTP request | Description
2121

2222
### Example
2323
```java
24-
// Import classes:{{#hasAuthMethods}}
25-
//import {{{invokerPackage}}}.ApiClient;{{/hasAuthMethods}}
26-
//import {{{invokerPackage}}}.ApiException;{{#hasAuthMethods}}
27-
//import {{{invokerPackage}}}.Configuration;
28-
//import {{{invokerPackage}}}.auth.*;{{/hasAuthMethods}}
29-
//import {{{package}}}.{{{classname}}};
30-
31-
{{#hasAuthMethods}}
32-
ApiClient defaultClient = Configuration.getDefaultApiClient();
33-
{{#authMethods}}{{#isBasic}}
34-
// Configure HTTP basic authorization: {{{name}}}
35-
HttpBasicAuth {{{name}}} = (HttpBasicAuth) defaultClient.getAuthentication("{{{name}}}");
36-
{{{name}}}.setUsername("YOUR USERNAME");
37-
{{{name}}}.setPassword("YOUR PASSWORD");{{/isBasic}}{{#isApiKey}}
38-
// Configure API key authorization: {{{name}}}
39-
ApiKeyAuth {{{name}}} = (ApiKeyAuth) defaultClient.getAuthentication("{{{name}}}");
40-
{{{name}}}.setApiKey("YOUR API KEY");
41-
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
42-
//{{{name}}}.setApiKeyPrefix("Token");{{/isApiKey}}{{#isOAuth}}
43-
// Configure OAuth2 access token for authorization: {{{name}}}
44-
OAuth {{{name}}} = (OAuth) defaultClient.getAuthentication("{{{name}}}");
45-
{{{name}}}.setAccessToken("YOUR ACCESS TOKEN");{{/isOAuth}}
46-
{{/authMethods}}
47-
{{/hasAuthMethods}}
48-
49-
{{{classname}}} apiInstance = new {{{classname}}}();
50-
{{#allParams}}
51-
{{{dataType}}} {{{paramName}}} = {{{example}}}; // {{{dataType}}} | {{{description}}}
52-
{{/allParams}}
53-
try {
54-
{{#returnType}}{{{returnType}}} result = {{/returnType}}apiInstance.{{{operationId}}}{{^vendorExtensions.x-group-parameters}}({{#allParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}({{#requiredParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/requiredParams}}){{#optionalParams}}
24+
// Import classes:
25+
import {{{invokerPackage}}}.ApiClient;
26+
import {{{invokerPackage}}}.ApiException;
27+
import {{{invokerPackage}}}.Configuration;{{#hasAuthMethods}}
28+
import {{{invokerPackage}}}.auth.*;{{/hasAuthMethods}}
29+
import {{{invokerPackage}}}.models.*;
30+
import {{{package}}}.{{{classname}}};
31+
32+
public class Example {
33+
public static void main(String[] args) {
34+
ApiClient defaultClient = Configuration.getDefaultApiClient();
35+
defaultClient.setBasePath("{{{basePath}}}");
36+
{{#hasAuthMethods}}
37+
{{#authMethods}}{{#isBasic}}
38+
// Configure HTTP basic authorization: {{{name}}}
39+
HttpBasicAuth {{{name}}} = (HttpBasicAuth) defaultClient.getAuthentication("{{{name}}}");
40+
{{{name}}}.setUsername("YOUR USERNAME");
41+
{{{name}}}.setPassword("YOUR PASSWORD");{{/isBasic}}{{#isApiKey}}
42+
// Configure API key authorization: {{{name}}}
43+
ApiKeyAuth {{{name}}} = (ApiKeyAuth) defaultClient.getAuthentication("{{{name}}}");
44+
{{{name}}}.setApiKey("YOUR API KEY");
45+
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
46+
//{{{name}}}.setApiKeyPrefix("Token");{{/isApiKey}}{{#isOAuth}}
47+
// Configure OAuth2 access token for authorization: {{{name}}}
48+
OAuth {{{name}}} = (OAuth) defaultClient.getAuthentication("{{{name}}}");
49+
{{{name}}}.setAccessToken("YOUR ACCESS TOKEN");{{/isOAuth}}
50+
{{/authMethods}}
51+
{{/hasAuthMethods}}
52+
53+
{{{classname}}} apiInstance = new {{{classname}}}(defaultClient);
54+
{{#allParams}}
55+
{{{dataType}}} {{{paramName}}} = {{{example}}}; // {{{dataType}}} | {{{description}}}
56+
{{/allParams}}
57+
try {
58+
{{#returnType}}{{{returnType}}} result = {{/returnType}}apiInstance.{{{operationId}}}{{^vendorExtensions.x-group-parameters}}({{#allParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}({{#requiredParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/requiredParams}}){{#optionalParams}}
5559
.{{{paramName}}}({{{paramName}}}){{/optionalParams}}
5660
.execute();{{/vendorExtensions.x-group-parameters}}{{#returnType}}
57-
System.out.println(result);{{/returnType}}
58-
} catch (ApiException e) {
59-
System.err.println("Exception when calling {{{classname}}}#{{{operationId}}}");
60-
e.printStackTrace();
61+
System.out.println(result);{{/returnType}}
62+
} catch (ApiException e) {
63+
System.err.println("Exception when calling {{{classname}}}#{{{operationId}}}");
64+
System.err.println("Status code: " + e.getCode());
65+
System.err.println("Reason: " + e.getResponseBody());
66+
System.err.println("Response headers: " + e.getResponseHeaders());
67+
e.printStackTrace();
68+
}
69+
}
6170
}
6271
```
6372

samples/client/petstore/java/okhttp-gson-parcelableModel/docs/AnotherFakeApi.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,30 @@ To test special tags and operation ID starting with number
1818
### Example
1919
```java
2020
// Import classes:
21-
//import org.openapitools.client.ApiException;
22-
//import org.openapitools.client.api.AnotherFakeApi;
23-
24-
25-
AnotherFakeApi apiInstance = new AnotherFakeApi();
26-
Client body = new Client(); // Client | client model
27-
try {
28-
Client result = apiInstance.call123testSpecialTags(body);
29-
System.out.println(result);
30-
} catch (ApiException e) {
31-
System.err.println("Exception when calling AnotherFakeApi#call123testSpecialTags");
32-
e.printStackTrace();
21+
import org.openapitools.client.ApiClient;
22+
import org.openapitools.client.ApiException;
23+
import org.openapitools.client.Configuration;
24+
import org.openapitools.client.models.*;
25+
import org.openapitools.client.api.AnotherFakeApi;
26+
27+
public class Example {
28+
public static void main(String[] args) {
29+
ApiClient defaultClient = Configuration.getDefaultApiClient();
30+
defaultClient.setBasePath("http://petstore.swagger.io:80/v2");
31+
32+
AnotherFakeApi apiInstance = new AnotherFakeApi(defaultClient);
33+
Client body = new Client(); // Client | client model
34+
try {
35+
Client result = apiInstance.call123testSpecialTags(body);
36+
System.out.println(result);
37+
} catch (ApiException e) {
38+
System.err.println("Exception when calling AnotherFakeApi#call123testSpecialTags");
39+
System.err.println("Status code: " + e.getCode());
40+
System.err.println("Reason: " + e.getResponseBody());
41+
System.err.println("Response headers: " + e.getResponseHeaders());
42+
e.printStackTrace();
43+
}
44+
}
3345
}
3446
```
3547

0 commit comments

Comments
 (0)