Skip to content

Commit 88e2673

Browse files
committed
Add auto-generated sample codes
1 parent 909916a commit 88e2673

File tree

646 files changed

+18153
-95
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

646 files changed

+18153
-95
lines changed

samples/client/echo_api/java/okhttp-gson-user-defined-templates/.openapi-generator/FILES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ api/openapi.yaml
55
build.gradle
66
build.sbt
77
docs/Category.md
8+
docs/Color.md
89
docs/ModelApiResponse.md
910
docs/Order.md
1011
docs/Pet.md
@@ -21,6 +22,7 @@ gradlew
2122
gradlew.bat
2223
info.md
2324
modelCopy/Category.java
25+
modelCopy/Color.java
2426
modelCopy/ModelApiResponse.java
2527
modelCopy/Order.java
2628
modelCopy/Pet.java
@@ -55,8 +57,10 @@ src/main/java/org/openapitools/client/auth/OAuthOkHttpClient.java
5557
src/main/java/org/openapitools/client/auth/RetryingOAuth.java
5658
src/main/java/org/openapitools/client/model/AbstractOpenApiSchema.java
5759
src/main/java/org/openapitools/client/model/Category.java
60+
src/main/java/org/openapitools/client/model/Color.java
5861
src/main/java/org/openapitools/client/model/ModelApiResponse.java
5962
src/main/java/org/openapitools/client/model/Order.java
6063
src/main/java/org/openapitools/client/model/Pet.java
6164
src/main/java/org/openapitools/client/model/Tag.java
6265
src/main/java/org/openapitools/client/model/User.java
66+
src/test/java/org/openapitools/client/model/ColorTest.java

samples/client/echo_api/java/okhttp-gson-user-defined-templates/api/openapi.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,38 @@ paths:
130130
x-accepts:
131131
- application/json
132132
- application/xml
133+
/pet/findByColor:
134+
get:
135+
description: Returns pets filtered by color.
136+
operationId: findPetsByColor
137+
parameters:
138+
- description: Color of the pet to filter by
139+
explode: true
140+
in: query
141+
name: color
142+
required: false
143+
schema:
144+
$ref: '#/components/schemas/Color'
145+
style: form
146+
responses:
147+
"200":
148+
content:
149+
application/json:
150+
schema:
151+
items:
152+
$ref: '#/components/schemas/Pet'
153+
type: array
154+
description: successful operation
155+
"400":
156+
description: Invalid color value
157+
security:
158+
- petstore_auth:
159+
- read:pets
160+
summary: Finds Pets by color
161+
tags:
162+
- pet
163+
x-accepts:
164+
- application/json
133165
/pet/findByTags:
134166
get:
135167
deprecated: true
@@ -819,6 +851,18 @@ components:
819851
type: object
820852
xml:
821853
name: Pet
854+
Color:
855+
description: pet color in the store
856+
enum:
857+
- black
858+
- white
859+
- brown
860+
- golden
861+
- mixed
862+
title: Pet Color
863+
type: string
864+
xml:
865+
name: Pet
822866
ApiResponse:
823867
description: Describes the result of uploading an image resource
824868
example:
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
3+
# Color
4+
5+
## Enum
6+
7+
8+
* `BLACK` (value: `"black"`)
9+
10+
* `WHITE` (value: `"white"`)
11+
12+
* `BROWN` (value: `"brown"`)
13+
14+
* `GOLDEN` (value: `"golden"`)
15+
16+
* `MIXED` (value: `"mixed"`)
17+
18+
19+

samples/client/echo_api/java/okhttp-gson-user-defined-templates/docs/PetApiDocumentation.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All URIs are relative to *http://petstore.swagger.io/v2*
66
|------------- | ------------- | -------------|
77
| [**addPet**](PetApi.md#addPet) | **POST** /pet | Add a new pet to the store |
88
| [**deletePet**](PetApi.md#deletePet) | **DELETE** /pet/{petId} | Deletes a pet |
9+
| [**findPetsByColor**](PetApi.md#findPetsByColor) | **GET** /pet/findByColor | Finds Pets by color |
910
| [**findPetsByStatus**](PetApi.md#findPetsByStatus) | **GET** /pet/findByStatus | Finds Pets by status |
1011
| [**findPetsByTags**](PetApi.md#findPetsByTags) | **GET** /pet/findByTags | Finds Pets by tags |
1112
| [**getPetById**](PetApi.md#getPetById) | **GET** /pet/{petId} | Find pet by ID |
@@ -150,6 +151,74 @@ null (empty response body)
150151
|-------------|-------------|------------------|
151152
| **400** | Invalid pet value | - |
152153

154+
<a id="findPetsByColor"></a>
155+
# **findPetsByColor**
156+
> List&lt;Pet&gt; findPetsByColor(color)
157+
158+
Finds Pets by color
159+
160+
Returns pets filtered by color.
161+
162+
### Example
163+
```java
164+
// Import classes:
165+
import org.openapitools.client.ApiClient;
166+
import org.openapitools.client.ApiException;
167+
import org.openapitools.client.Configuration;
168+
import org.openapitools.client.auth.*;
169+
import org.openapitools.client.models.*;
170+
import org.openapitools.client.api.PetApi;
171+
172+
public class Example {
173+
public static void main(String[] args) {
174+
ApiClient defaultClient = Configuration.getDefaultApiClient();
175+
defaultClient.setBasePath("http://petstore.swagger.io/v2");
176+
177+
// Configure OAuth2 access token for authorization: petstore_auth
178+
OAuth petstore_auth = (OAuth) defaultClient.getAuthentication("petstore_auth");
179+
petstore_auth.setAccessToken("YOUR ACCESS TOKEN");
180+
181+
PetApi apiInstance = new PetApi(defaultClient);
182+
Color color = Color.fromValue("black"); // Color | Color of the pet to filter by
183+
try {
184+
List<Pet> result = apiInstance.findPetsByColor(color);
185+
System.out.println(result);
186+
} catch (ApiException e) {
187+
System.err.println("Exception when calling PetApi#findPetsByColor");
188+
System.err.println("Status code: " + e.getCode());
189+
System.err.println("Reason: " + e.getResponseBody());
190+
System.err.println("Response headers: " + e.getResponseHeaders());
191+
e.printStackTrace();
192+
}
193+
}
194+
}
195+
```
196+
197+
### Parameters
198+
199+
| Name | Type | Description | Notes |
200+
|------------- | ------------- | ------------- | -------------|
201+
| **color** | [**Color**](.md)| Color of the pet to filter by | [optional] [enum: black, white, brown, golden, mixed] |
202+
203+
### Return type
204+
205+
[**List&lt;Pet&gt;**](Pet.md)
206+
207+
### Authorization
208+
209+
[petstore_auth](../README.md#petstore_auth)
210+
211+
### HTTP request headers
212+
213+
- **Content-Type**: Not defined
214+
- **Accept**: application/json
215+
216+
### HTTP response details
217+
| Status code | Description | Response headers |
218+
|-------------|-------------|------------------|
219+
| **200** | successful operation | - |
220+
| **400** | Invalid color value | - |
221+
153222
<a id="findPetsByStatus"></a>
154223
# **findPetsByStatus**
155224
> List&lt;Pet&gt; findPetsByStatus(status)

samples/client/echo_api/java/okhttp-gson-user-defined-templates/info.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Class | Method | HTTP request | Description
121121
------------ | ------------- | ------------- | -------------
122122
*PetApi* | [**addPet**](docs/PetApi.md#addPet) | **POST** /pet | Add a new pet to the store
123123
*PetApi* | [**deletePet**](docs/PetApi.md#deletePet) | **DELETE** /pet/{petId} | Deletes a pet
124+
*PetApi* | [**findPetsByColor**](docs/PetApi.md#findPetsByColor) | **GET** /pet/findByColor | Finds Pets by color
124125
*PetApi* | [**findPetsByStatus**](docs/PetApi.md#findPetsByStatus) | **GET** /pet/findByStatus | Finds Pets by status
125126
*PetApi* | [**findPetsByTags**](docs/PetApi.md#findPetsByTags) | **GET** /pet/findByTags | Finds Pets by tags
126127
*PetApi* | [**getPetById**](docs/PetApi.md#getPetById) | **GET** /pet/{petId} | Find pet by ID
@@ -144,6 +145,7 @@ Class | Method | HTTP request | Description
144145
## Documentation for Models
145146

146147
- [Category](docs/Category.md)
148+
- [Color](docs/Color.md)
147149
- [ModelApiResponse](docs/ModelApiResponse.md)
148150
- [Order](docs/Order.md)
149151
- [Pet](docs/Pet.md)
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import com.google.gson.Gson;
2+
import com.google.gson.GsonBuilder;
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonDeserializationContext;
5+
import com.google.gson.JsonDeserializer;
6+
import com.google.gson.JsonElement;
7+
import com.google.gson.JsonObject;
8+
import com.google.gson.JsonParseException;
9+
import com.google.gson.TypeAdapterFactory;
10+
import com.google.gson.reflect.TypeToken;
11+
import com.google.gson.TypeAdapter;
12+
import com.google.gson.stream.JsonReader;
13+
import com.google.gson.stream.JsonWriter;
14+
import java.io.IOException;
15+
16+
import java.util.HashMap;
17+
import java.util.HashSet;
18+
import java.util.List;
19+
import java.util.Map;
20+
import java.util.Set;
21+
22+
import org.openapitools.client.JSON;
23+
24+
/**
25+
* Color
26+
*/
27+
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.13.0-SNAPSHOT")
28+
public class Color {
29+
public Color() {
30+
}
31+
32+
33+
@Override
34+
public boolean equals(Object o) {
35+
if (this == o) {
36+
return true;
37+
}
38+
if (o == null || getClass() != o.getClass()) {
39+
return false;
40+
}
41+
return true;
42+
}
43+
44+
@Override
45+
public int hashCode() {
46+
return Objects.hash();
47+
}
48+
49+
@Override
50+
public String toString() {
51+
StringBuilder sb = new StringBuilder();
52+
sb.append("class Color {\n");
53+
sb.append("}");
54+
return sb.toString();
55+
}
56+
57+
/**
58+
* Convert the given object to string with each line indented by 4 spaces
59+
* (except the first line).
60+
*/
61+
private String toIndentedString(Object o) {
62+
if (o == null) {
63+
return "null";
64+
}
65+
return o.toString().replace("\n", "\n ");
66+
}
67+
68+
69+
public static HashSet<String> openapiFields;
70+
public static HashSet<String> openapiRequiredFields;
71+
72+
static {
73+
// a set of all properties/fields (JSON key names)
74+
openapiFields = new HashSet<String>();
75+
76+
// a set of required properties/fields (JSON key names)
77+
openapiRequiredFields = new HashSet<String>();
78+
}
79+
80+
/**
81+
* Validates the JSON Element and throws an exception if issues found
82+
*
83+
* @param jsonElement JSON Element
84+
* @throws IOException if the JSON Element is invalid with respect to Color
85+
*/
86+
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
87+
if (jsonElement == null) {
88+
if (!Color.openapiRequiredFields.isEmpty()) { // has required fields but JSON element is null
89+
throw new IllegalArgumentException(String.format("The required field(s) %s in Color is not found in the empty JSON string", Color.openapiRequiredFields.toString()));
90+
}
91+
}
92+
93+
Set<Map.Entry<String, JsonElement>> entries = jsonElement.getAsJsonObject().entrySet();
94+
// check to see if the JSON string contains additional fields
95+
for (Map.Entry<String, JsonElement> entry : entries) {
96+
if (!Color.openapiFields.contains(entry.getKey())) {
97+
throw new IllegalArgumentException(String.format("The field `%s` in the JSON string is not defined in the `Color` properties. JSON: %s", entry.getKey(), jsonElement.toString()));
98+
}
99+
}
100+
}
101+
102+
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
103+
@SuppressWarnings("unchecked")
104+
@Override
105+
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
106+
if (!Color.class.isAssignableFrom(type.getRawType())) {
107+
return null; // this class only serializes 'Color' and its subtypes
108+
}
109+
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
110+
final TypeAdapter<Color> thisAdapter
111+
= gson.getDelegateAdapter(this, TypeToken.get(Color.class));
112+
113+
return (TypeAdapter<T>) new TypeAdapter<Color>() {
114+
@Override
115+
public void write(JsonWriter out, Color value) throws IOException {
116+
JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject();
117+
elementAdapter.write(out, obj);
118+
}
119+
120+
@Override
121+
public Color read(JsonReader in) throws IOException {
122+
JsonElement jsonElement = elementAdapter.read(in);
123+
validateJsonElement(jsonElement);
124+
return thisAdapter.fromJsonTree(jsonElement);
125+
}
126+
127+
}.nullSafe();
128+
}
129+
}
130+
131+
/**
132+
* Create an instance of Color given an JSON string
133+
*
134+
* @param jsonString JSON string
135+
* @return An instance of Color
136+
* @throws IOException if the JSON string is invalid with respect to Color
137+
*/
138+
public static Color fromJson(String jsonString) throws IOException {
139+
return JSON.getGson().fromJson(jsonString, Color.class);
140+
}
141+
142+
/**
143+
* Convert an instance of Color to an JSON string
144+
*
145+
* @return JSON string
146+
*/
147+
public String toJson() {
148+
return JSON.getGson().toJson(this);
149+
}
150+
}

0 commit comments

Comments
 (0)