Skip to content
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
Expand Up @@ -231,4 +231,15 @@ public String toDefaultValue(Schema schema) {
}
return super.toDefaultValue(schema);
}

@Override
public String toEnumVarName(String value, String datatype) {
if ("String".equals(datatype)) {
// convert camelCase77String to CAMEL_CASE_77_STRING
return value
.replaceAll("(.+?)([A-Z]|[0-9])", "$1_$2")
.toUpperCase(Locale.ROOT);
}
return super.toEnumVarName(value, datatype);
}
}
2 changes: 1 addition & 1 deletion scripts/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function playground({
});
break;
case 'java':
await run(`./gradle/gradlew --no-daemon -p playground/java run`, {
await run(`./gradle/gradlew -p playground/java run`, {
verbose,
});
break;
Expand Down
108 changes: 21 additions & 87 deletions templates/java/JSON.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Date;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Hashtable;
Expand Down Expand Up @@ -52,13 +47,11 @@ import okio.ByteString;

public class JSON {
private static Gson gson;
private static DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();
private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();
private static RetainFieldMapFactory mapAdapter = new RetainFieldMapFactory();
private static final ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();
private static final RetainFieldMapFactory mapAdapter = new RetainFieldMapFactory();

static {
gson = createGson()
.registerTypeAdapter(Date.class, dateTypeAdapter)
.registerTypeAdapter(byte[].class, byteArrayAdapter)
.registerTypeAdapterFactory(mapAdapter)
.create();
Expand Down Expand Up @@ -87,6 +80,11 @@ public class JSON {
return fireBuilder.createGsonBuilder();
}

// Suppress default constructor for noninstantiability
private JSON() {
throw new AssertionError();
}

/**
* Get Gson.
*
Expand Down Expand Up @@ -137,95 +135,31 @@ public class JSON {
}
}
}

public static void setDateFormat(DateFormat dateFormat) {
dateTypeAdapter.setFormat(dateFormat);
}
}

/**
* Gson TypeAdapter for Byte Array type
*/
class ByteArrayAdapter extends TypeAdapter<byte[]> {

@Override
public void write(JsonWriter out, byte[] value) throws IOException {
@Override
public void write(JsonWriter out, byte[] value) throws IOException {
if (value == null) {
out.nullValue();
} else {
out.value(ByteString.of(value).base64());
}
}

@Override
public byte[] read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String bytesAsBase64 = in.nextString();
ByteString byteString = ByteString.decodeBase64(bytesAsBase64);
return byteString.toByteArray();
}
}
}

/**
* Gson TypeAdapter for java.util.Date type
* If the dateFormat is null, ISO8601Utils will be used.
*/
class DateTypeAdapter extends TypeAdapter<Date> {

private DateFormat dateFormat;

public DateTypeAdapter() {}

public DateTypeAdapter(DateFormat dateFormat) {
this.dateFormat = dateFormat;
}

public void setFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat;
}

@Override
public void write(JsonWriter out, Date date) throws IOException {
if (date == null) {
out.nullValue();
out.nullValue();
} else {
String value;
if (dateFormat != null) {
value = dateFormat.format(date);
} else {
value = ISO8601Utils.format(date, true);
}
out.value(value);
out.value(ByteString.of(value).base64());
}
}
}

@Override
public Date read(JsonReader in) throws IOException {
try {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
try {
if (dateFormat != null) {
return dateFormat.parse(date);
}
return ISO8601Utils.parse(date, new ParsePosition(0));
} catch (ParseException e) {
throw new JsonParseException(e);
}
}
} catch (IllegalArgumentException e) {
throw new JsonParseException(e);
@Override
public byte[] read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
}
String bytesAsBase64 = in.nextString();
ByteString byteString = ByteString.decodeBase64(bytesAsBase64);
return byteString != null ? byteString.toByteArray() : new byte[0];
}
}

// https://stackoverflow.com/questions/21458468/gson-wont-properly-serialise-a-class-that-extends-hashmap
Expand Down
6 changes: 3 additions & 3 deletions templates/java/build.gradle.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'java'
id 'java-library'
id 'maven-publish'
}

Expand All @@ -14,9 +14,9 @@ repositories {

dependencies {
implementation 'com.google.code.findbugs:jsr305:3.0.2'
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
api 'com.squareup.okhttp3:okhttp:4.9.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'
implementation 'com.google.code.gson:gson:2.8.9'
api 'com.google.code.gson:gson:2.8.9'
implementation 'io.gsonfire:gson-fire:1.8.5'
}

Expand Down
18 changes: 9 additions & 9 deletions templates/java/libraries/okhttp-gson/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class {{classname}} extends ApiClient {
{{#operation}}
/**
* Build call for {{operationId}}
* @param _callback Callback for upload/download progress
* @param callback Callback for upload/download progress
* @return Call to execute
* @throws AlgoliaRuntimeException If fail to serialize the request body object
{{#isDeprecated}}
Expand All @@ -107,7 +107,7 @@ public class {{classname}} extends ApiClient {
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
private Call {{operationId}}Call({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ApiCallback<{{{returnType}}}> _callback) throws AlgoliaRuntimeException {
private Call {{operationId}}Call({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ApiCallback<{{{returnType}}}> callback) throws AlgoliaRuntimeException {
Object bodyObj = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};

// create path and map variables
Expand Down Expand Up @@ -141,21 +141,21 @@ public class {{classname}} extends ApiClient {
headers.put("Accept", "application/json");
headers.put("Content-Type", "application/json");

return this.buildCall(requestPath, "{{httpMethod}}", queryParams, bodyObj, headers, _callback);
return this.buildCall(requestPath, "{{httpMethod}}", queryParams, bodyObj, headers, callback);
}

{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
private Call {{operationId}}ValidateBeforeCall({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ApiCallback<{{{returnType}}}> _callback) throws AlgoliaRuntimeException {
private Call {{operationId}}ValidateBeforeCall({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ApiCallback<{{{returnType}}}> callback) throws AlgoliaRuntimeException {
{{#allParams}}{{#required}}
// verify the required parameter '{{paramName}}' is set
if ({{paramName}} == null) {
throw new AlgoliaRuntimeException("Missing the required parameter '{{paramName}}' when calling {{operationId}}(Async)");
}
{{/required}}{{/allParams}}

return {{operationId}}Call({{#allParams}}{{paramName}}, {{/allParams}}_callback);
return {{operationId}}Call({{#allParams}}{{paramName}}, {{/allParams}}callback);
}

/**
Expand Down Expand Up @@ -195,7 +195,7 @@ public class {{classname}} extends ApiClient {
* (asynchronously)
* {{notes}}{{#allParams}}
* @param {{paramName}} {{{description}}}{{#required}} (required){{/required}}{{^required}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}}){{/required}}{{/allParams}}
* @param _callback The callback to be executed when the API call finishes
* @param callback The callback to be executed when the API call finishes
* @return The request call
* @throws AlgoliaRuntimeException If fail to process the API call, e.g. serializing the request body object
{{#isDeprecated}}
Expand All @@ -209,10 +209,10 @@ public class {{classname}} extends ApiClient {
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public Call {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ApiCallback<{{{returnType}}}{{^returnType}}Void{{/returnType}}> _callback) throws AlgoliaRuntimeException {
Call call = {{operationId}}ValidateBeforeCall({{#allParams}}{{paramName}}, {{/allParams}}_callback);
public Call {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ApiCallback<{{{returnType}}}{{^returnType}}Void{{/returnType}}> callback) throws AlgoliaRuntimeException {
Call call = {{operationId}}ValidateBeforeCall({{#allParams}}{{paramName}}, {{/allParams}}callback);
{{#returnType}}Type returnType = new TypeToken<{{{returnType}}}>(){}.getType();
this.executeAsync(call, returnType, _callback);{{/returnType}}{{^returnType}}this.executeAsync(call, _callback);{{/returnType}}
this.executeAsync(call, returnType, callback);{{/returnType}}{{^returnType}}this.executeAsync(call, callback);{{/returnType}}
return call;
}
{{/operation}}
Expand Down
2 changes: 1 addition & 1 deletion templates/java/modelEnum.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.google.gson.stream.JsonWriter;
*/{{/enumDescription}}
{{{name}}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}}

private {{{dataType}}} value;
private final {{{dataType}}} value;

{{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}({{{dataType}}} value) {
this.value = value;
Expand Down
1 change: 0 additions & 1 deletion templates/java/oneof_interface.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public abstract class {{classname}} implements CompoundType {
}

{{/vendorExtensions.x-is-one-of-list}}
public abstract Object getInsideValue();

public static class Adapter extends TypeAdapter<{{classname}}> {
@Override
Expand Down
8 changes: 1 addition & 7 deletions templates/java/pojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
{{#vars}}

{{^isReadOnly}}
public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) {
public {{classname}} {{setter}}({{{datatypeWithEnum}}} {{name}}) {
this.{{name}} = {{name}};
return this;
}
Expand Down Expand Up @@ -124,12 +124,6 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
return {{name}};
}

{{^isReadOnly}}
public void {{setter}}({{{datatypeWithEnum}}} {{name}}) {
this.{{name}} = {{name}};
}
{{/isReadOnly}}

{{/vars}}

@Override
Expand Down