Skip to content

Commit

Permalink
Merge pull request #280 from SUSE/handle-empty-strings
Browse files Browse the repository at this point in the history
Change empty strings to optional.empty
  • Loading branch information
admd authored Feb 26, 2020
2 parents ba9df39 + 3847ef5 commit 4b02ec1
Showing 1 changed file with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.suse.salt.netapi.parser;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.bind.TypeAdapters;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
Expand Down Expand Up @@ -41,8 +44,24 @@ public Optional<A> read(JsonReader in) throws IOException {
in.nextNull();
return Optional.empty();
} else {
A value = innerAdapter.read(in);
return Optional.of(value);
JsonElement json = TypeAdapters.JSON_ELEMENT.read(in);
try {
A value = innerAdapter.fromJsonTree(json);
return Optional.of(value);
}
catch (JsonSyntaxException e) {
/**
* Note : This is a workaround and it only exists because salt doesn't differentiate between a
* non-existent grain and a grain which exists but has value set to empty String.
*
* If an object is expected but instead empty string comes in then we return empty Optional.
*/
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isString() &&
json.getAsString().isEmpty()) {
return Optional.empty();
}
throw e;
}
}
}

Expand Down

0 comments on commit 4b02ec1

Please sign in to comment.