-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modified Java code to allow passing Null as valid input for desired a…
…nd reported properties (#216) * Modified Java code to allow passing Null as valid input for desired and reported properties * Renamed variableIsNullValid to variableIsNullable for better readability. Added setting IsNullable to parsing from payload * Removed legacy leftovers from a previous test attempt at passing Null
- Loading branch information
1 parent
2e88250
commit 6b711b1
Showing
30 changed files
with
184 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
sdk/src/main/java/software/amazon/awssdk/iot/ShadowStateFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package software.amazon.awssdk.iot; | ||
|
||
import software.amazon.awssdk.iot.iotshadow.model.ShadowState; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.TypeAdapterFactory; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
public class ShadowStateFactory implements TypeAdapterFactory { | ||
|
||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | ||
|
||
Class<T> rawType = (Class<T>)type.getRawType(); | ||
if (rawType != ShadowState.class) { | ||
return null; | ||
} | ||
|
||
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); | ||
|
||
return new TypeAdapter<T>() { | ||
public void write(JsonWriter out, T shadowValue) throws IOException { | ||
// Are null values allowed? | ||
ShadowState shadow = (ShadowState)shadowValue; | ||
if (shadow.desiredIsNullable == true || shadow.reportedIsNullable == true) { | ||
out.setSerializeNulls(true); | ||
} | ||
// If a property is null but null is not valid for it, then just send an empty HashMap | ||
if (shadow.desired == null && shadow.desiredIsNullable == false) { | ||
shadow.desired = new HashMap<String, Object>(); | ||
} | ||
if (shadow.reported == null && shadow.reportedIsNullable == false) { | ||
shadow.reported = new HashMap<String, Object>(); | ||
} | ||
delegate.write(out, shadowValue); | ||
if (shadow.desiredIsNullable == true || shadow.reportedIsNullable == true) { | ||
out.setSerializeNulls(false); | ||
} | ||
} | ||
public T read(JsonReader in) throws IOException { | ||
T returnType = delegate.read(in); | ||
|
||
// Set <Name>IsNullable if the field we get is null | ||
ShadowState shadow = (ShadowState)returnType; | ||
if (shadow.desired == null) { | ||
shadow.desiredIsNullable = true; | ||
} | ||
if (shadow.reported == null) { | ||
shadow.reportedIsNullable = true; | ||
} | ||
returnType = (T)shadow; | ||
|
||
return returnType; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,5 @@ public class DeleteShadowSubscriptionRequest { | |
*/ | ||
public String thingName; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,5 @@ public class GetShadowSubscriptionRequest { | |
*/ | ||
public String thingName; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.