The generated code samples are now idiomatic with no errors. This improves the developer experience by allowing developers to run the code samples without any error in their applications and to get started quickly.
Details
This release introduces significant improvements to the code samples generated for your API. We've made several updates to the code samples to ensure that they are idiomatic, easier to understand and production-ready. The new code samples are designed to enhance the developer experience that will allow them to grasp the intention of an endpoint as to what are the parameters, how they are being initialized. This will let them get started with your API quickly and easily by skimming through the code samples.
What Has Changed?
To meet the standards, the updated code samples are passed through the linter i.e. CheckStyle (Click here to see the configuration being used).
Following major improvements are now made part of the Java code samples:
- Simplified code samples to make them easier to read and understand with the help of properly indented code samples and inline-initialization of the parameters.
- Removed unused imports for models and utility classes.
- Fixed linting issues in the updated code samples to ensure that they comply with the best practices in terms of coding styles and standards.
- Fixed bugs and errors in the code samples to ensure that they work as expected.
Imports Section Improvements
Wildcard Imports
Wildcard imports have been replaced with the specific imports.
- Before
- After
import java.util.*;
import localhost3000.*;
import localhost3000.models.*;
import localhost3000.controllers.*;
import localhost3000.TesterClient;
import localhost3000.controllers.ResponseTypesController;
import localhost3000.models.Employee;
import java.util.List;
import java.util.Map;
import java.util.Arrays;
import localhost3000.models.Person;
import localhost3000.DateTimeHelper;
import localhost3000.models.Days;
import java.util.LinkedHashMap;
Unused Imports
Previously imports are added even if they are not required in any other sections, but now we are adding imports on demand.
// imports section
import java.util.List;
import java.util.Arrays;
// parameters initialization section
List<String> optionalParam = Arrays.asList(
"Bob",
"Alice",
"John",
"Doe"
);
String requiredParam = 'some string value';
After removing the Optional parameter optionalParam
from the API call, all related imports will vanish:
// empty imports section
// parameters initialization section
String requiredParam = 'some string value';
Parameters Initialization Section Improvements
Model Initialization
The in-line initlization has been added to ensure that no intermediate variables should be used in the code samples and they are easier to understand, and more user-friendly.
- Before
- After
Employee model = new Employee();
model.setName("John");
model.setHiredAt(DateTimeHelper.fromRfc1123DateTime("Mon, 15 Jun 2009 20:45:30 GMT"));
Person boss = new Person();
boss.setName("Doe");
boss.setAge(40);
model.setBoss(boss);
Employee model = new Employee.Builder(
"John",
DateTimeHelper.fromRfc1123DateTime("Mon, 15 Jun 2009 20:45:30 GMT"),
new Person.Builder(
"Doe",
40
)
.build()
)
.build();
Date Initialization
Now the code samples make use of the utility method for parsing a date.
- Before
- After
LocalDate dateOfBirth = LocalDate.parse("2016-03-13");
LocalDate dateOfBirth = DateTimeHelper.fromSimpleDate("2016-03-13");
DateTime Initialization
Now the code samples make use of the utility methods for parsing date-time of different formats.
RFC-3339 Initialization
- Before
- After
LocalDateTime rfc3339DateTime = LocalDateTime.parse("2016-03-13T12:52:32.123Z", DateTimeFormatter.ISO_DATE_TIME);
LocalDateTime rfc3339DateTime = DateTimeHelper.fromRfc8601DateTime("2016-03-13T12:52:32.123Z");
UNIX Timestamp Initialization
- Before
- After
LocalDateTime unixTimestamp = LocalDateTime.ofInstant(Instant.ofEpochSecond(1480809600), ZoneId.systemDefault());
LocalDateTime unixTimestamp = DateTimeHelper.fromUnixTimestamp(1480809600L);
Array Initialization
Example 1
- Before
- After
List<Integer> stringArray = new LinkedList<>();
stringArray.add("Bob");
stringArray.add("Alice");
stringArray.add("John");
stringArray.add("Doe");
List<String> stringArray = Arrays.asList(
"Bob",
"Alice",
"John",
"Doe"
);
Example 2
- Before
- After
List<Map<String, Integer>> numberArrayOfMap = new LinkedList<>();
Map<String, Integer> numberArrayOfMap0 = new LinkedHashMap<>();
numberArrayOfMap0.put("key0", 216);
numberArrayOfMap.add(numberArrayOfMap0);
Map<String, Integer> numberArrayOfMap1 = new LinkedHashMap<>();
numberArrayOfMap1.put("key0", 217);
numberArrayOfMap1.put("key1", 218);
numberArrayOfMap.add(numberArrayOfMap1);
List<Map<String, Integer>> numberArrayOfMap = Arrays.asList(
new LinkedHashMap<String, Integer>() {{
put("key0", 216);
}},
new LinkedHashMap<String, Integer>() {{
put("key0", 217);
put("key1", 218);
}}
);
Map Initialization
Example 1
- Before
- After
Map<String, Integer> numberMap = new LinkedHashMap<>();
numberMap.put("key0", 110);
numberMap.put("key1", 111);
numberMap.put("key2", 112);
Map<String, Integer> numberMap = new LinkedHashMap<String, Integer>() {{
put("key0", 110);
put("key1", 111);
put("key2", 112);
}};
Example 2
- Before
- After
Map<String, List<Integer>> numberMapOfArray = new LinkedHashMap<>();
List<Integer> numberMapOfArray0 = new LinkedList<>();
numberMapOfArray0.add(0);
numberMapOfArray0.add(1);
numberMapOfArray.put("key0", numberMapOfArray0);
List<Integer> numberMapOfArray1 = new LinkedList<>();
numberMapOfArray1.add(1);
numberMapOfArray1.add(2);
numberMapOfArray1.add(3);
numberMapOfArray.put("key1", numberMapOfArray1);
List<Integer> numberMapOfArray2 = new LinkedList<>();
numberMapOfArray2.add(2);
numberMapOfArray.put("key2", numberMapOfArray2);
Map<String, List<Integer>> numberMapOfArray = new LinkedHashMap<String, List<Integer>>() {{
put("key0", Arrays.asList(
0,
1
));
put("key1", Arrays.asList(
1,
2,
3
));
put("key2", Arrays.asList(
2
));
}};
Multi-Dimensional Array Initialization
- Before
- After
List<List<Integer>> numbers2dArray = new LinkedList<>();
List<Integer> numbers2dArray0 = new LinkedList<>();
numbers2dArray0.add(158);
numbers2dArray0.add(160);
numbers2dArray.add(numbers2dArray0);
List<Integer> numbers2dArray1 = new LinkedList<>();
numbers2dArray1.add(168);
numbers2dArray1.add(170);
numbers2dArray.add(numbers2dArray1);
List<List<Integer>> numbers2dArray = Arrays.asList(
Arrays.asList(
158,
160
),
Arrays.asList(
168,
170
)
);
API Call Section Improvements
The API call will print the response in case of success or exception in case of an error.
- Before
- After
ResponseTypesController responseTypesController = client.getResponseTypesController();
responseTypesController.sendDataAsync(param1, param2, param3).thenAccept(result -> {
// TODO success callback handler
}).exceptionally(exception -> {
// TODO failure callback handler
return null;
});
ResponseTypesController responseTypesController = client.getResponseTypesController();
responseTypesController.sendDataAsync(param1, param2, param3).thenAccept(result -> {
// TODO success callback handler
System.out.println(result);
}).exceptionally(exception -> {
// TODO failure callback handler
exception.printStackTrace();
return null;
});
A Comparison of Complete Example Code Sample
The following code shows the complete comparison between the code sample generation for initializing an employee model as an input parameter for ResponseTypesController Send
endpoint.
Employee Model
"employee_model": {
"name": "John",
"hiredAt": "Mon, 15 Jun 2009 20:45:30 GMT",
"salary": null,
"boss": {
"name": "Doe",
"age": 40
}
}
- Before
- After
package com.example;
import java.util.*;
import java.io.*;
import localhost3000.*;
import localhost3000.models.*;
import localhost3000.controllers.*;
import localhost3000.exceptions.ApiException;
import java.time.LocalDateTime;
public class Program {
public static void main(String[] args) {
TesterClient client = new TesterClient.Builder()
.httpClientConfig(configBuilder -> configBuilder
.timeout(0))
.suites(SuiteCode.HEARTS)
.build();
ResponseTypesController responseTypesController = client.getResponseTypesController();
Employee model = new Employee();
model.setName("John");
model.setHiredAt(DateTimeHelper.fromRfc1123DateTime("Mon, 15 Jun 2009 20:45:30 GMT"));
Person boss = new Person();
boss.setName("Doe");
boss.setAge(40);
model.setBoss(boss);
responseTypesController.sendAsync(model).thenAccept(result -> {
// TODO success callback handler
}).exceptionally(exception -> {
// TODO failure callback handler
return null;
});
}
}
package com.example;
import localhost3000.TesterClient;
import localhost3000.controllers.ResponseTypesController;
import localhost3000.models.Employee;
import localhost3000.models.Person;
import localhost3000.DateTimeHelper;
public class Program {
public static void main(String[] args) {
TesterClient client = new TesterClient.Builder()
.httpClientConfig(configBuilder -> configBuilder
.timeout(0))
.port("80")
.build();
ResponseTypesController responseTypesController = client.getResponseTypesController();
Employee model = new Employee.Builder(
"John",
DateTimeHelper.fromRfc1123DateTime("Mon, 15 Jun 2009 20:45:30 GMT"),
new Person.Builder(
"Doe",
40
)
.build()
)
.build();
responseTypesController.sendAsync(model).thenAccept(response -> {
// TODO success callback handler
System.out.println(response);
}).exceptionally(exception -> {
// TODO failure callback handler
exception.printStackTrace();
return null;
});
}
}
Bugs Fixed
- Fixed an issue with
void
type response handling for synchronous API calls.