Skip to content
Permalink
Browse files
fix: Remove default value handling (#1353)
This is done in favor of explicit `optional` field handling in the generator.
  • Loading branch information
vam-google committed Apr 30, 2021
1 parent 06a53ac commit ed0fc791b22db45bd20de890b0abecd1839d2d86
@@ -101,9 +101,6 @@ RequestT fromJson(InputStream message, Charset messageCharset, Message.Builder b
* @param fieldValue a field value
*/
public void putPathParam(Map<String, String> fields, String fieldName, Object fieldValue) {
if (isDefaultValue(fieldName, fieldValue)) {
return;
}
fields.put(fieldName, String.valueOf(fieldValue));
}

@@ -116,11 +113,6 @@ public void putPathParam(Map<String, String> fields, String fieldName, Object fi
* @param fieldValue a field value
*/
public void putQueryParam(Map<String, List<String>> fields, String fieldName, Object fieldValue) {
// Avoids empty query parameter
if (isDefaultValue(fieldName, fieldValue)) {
return;
}

ImmutableList.Builder<String> paramValueList = ImmutableList.builder();
if (fieldValue instanceof List<?>) {
for (Object fieldValueItem : (List<?>) fieldValue) {
@@ -142,15 +134,4 @@ public void putQueryParam(Map<String, List<String>> fields, String fieldName, Ob
public String toBody(String fieldName, RequestT fieldValue) {
return toJson(fieldValue);
}

private boolean isDefaultValue(String fieldName, Object fieldValue) {
// TODO: Revisit this approach to ensure proper default-value handling as per design.
if (fieldValue instanceof Number) {
return ((Number) fieldValue).longValue() == 0L;
} else if (fieldValue instanceof String) {
return ((String) fieldValue).isEmpty();
}

return false;
}
}
@@ -98,6 +98,7 @@ public void getQueryParamNames() {
Map<String, List<String>> queryParamNames = formatter.getQueryParamNames(field);
Map<String, List<String>> expected = new HashMap<>();
expected.put("number", Arrays.asList("2"));
expected.put("typeUrl", Arrays.asList(""));
Truth.assertThat(queryParamNames).isEqualTo(expected);
}

@@ -115,7 +115,9 @@ public void putPathParam() {

Map<String, String> expectedFields = new HashMap<>();
expectedFields.put("optName1", "1");
expectedFields.put("optName2", "0");
expectedFields.put("optName3", "three");
expectedFields.put("optName4", "");

Truth.assertThat(fields).isEqualTo(expectedFields);
}
@@ -131,7 +133,9 @@ public void putQueryParam() {

Map<String, List<String>> expectedFields = new HashMap<>();
expectedFields.put("optName1", Arrays.asList("1"));
expectedFields.put("optName2", Arrays.asList("0"));
expectedFields.put("optName3", Arrays.asList("three"));
expectedFields.put("optName4", Arrays.asList(""));
expectedFields.put("optName5", Arrays.asList("four", "five"));

Truth.assertThat(fields).isEqualTo(expectedFields);

0 comments on commit ed0fc79

Please sign in to comment.