Skip to content

Commit

Permalink
Do not migrate valid reply-target without "enabled=true".
Browse files Browse the repository at this point in the history
Signed-off-by: Yufei Cai <yufei.cai@bosch-si.com>
  • Loading branch information
yufei-cai committed Nov 15, 2019
1 parent 4c9e645 commit e8460f0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonMissingFieldException;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;
Expand Down Expand Up @@ -70,23 +71,18 @@ public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<
return jsonObjectBuilder.build();
}

/**
* Creates a new reply-target object from the specified JSON object.
*
* @param jsonObject a JSON object which provides the data for the Target to be created.
* @return a new Target which is initialised with the extracted data from {@code jsonObject}.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} is not an appropriate JSON object.
*/
static ReplyTarget fromJson(final JsonObject jsonObject) {
final HeaderMapping readHeaderMapping = jsonObject.getValue(JsonFields.HEADER_MAPPING)
.map(ConnectivityModelFactory::newHeaderMapping)
.orElse(null);
return fromJsonOptional(jsonObject).orElseThrow(() -> new JsonMissingFieldException(JsonFields.ADDRESS));
}

static Optional<ReplyTarget> fromJsonOptional(final JsonObject jsonObject) {
return jsonObject.getValue(JsonFields.ADDRESS).map(address -> new Builder()
.address(address)
.headerMapping(jsonObject.getValue(JsonFields.HEADER_MAPPING)
.map(ConnectivityModelFactory::newHeaderMapping)
.orElse(null))
.build());

return new Builder()
.address(jsonObject.getValueOrThrow(JsonFields.ADDRESS))
.headerMapping(readHeaderMapping)
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,11 @@ public static Source fromJson(final JsonObject jsonObject, final int index) {
.map(ImmutablePayloadMapping::fromJson)
.orElse(ConnectivityModelFactory.emptyPayloadMapping());

final Optional<Boolean> replyTargetEnabled = jsonObject.getValue(JsonFields.REPLY_TARGET_ENABLED);
final boolean replyTargetEnabled =
jsonObject.getValue(JsonFields.REPLY_TARGET_ENABLED).orElse(DEFAULT_REPLY_TARGET_ENABLED);

// deserialize reply-target only if reply target is explicitly enabled
final ReplyTarget readReplyTarget = replyTargetEnabled.orElse(false)
? jsonObject.getValue(JsonFields.REPLY_TARGET).map(ReplyTarget::fromJson).orElse(null)
: null;
final ReplyTarget readReplyTarget =
jsonObject.getValue(JsonFields.REPLY_TARGET).flatMap(ReplyTarget::fromJsonOptional).orElse(null);

return new Builder()
.addresses(readSources)
Expand All @@ -249,7 +248,7 @@ public static Source fromJson(final JsonObject jsonObject, final int index) {
.enforcement(readEnforcement)
.headerMapping(readHeaderMapping)
.payloadMapping(readPayloadMapping)
.replyTargetEnabled(replyTargetEnabled.orElse(DEFAULT_REPLY_TARGET_ENABLED))
.replyTargetEnabled(replyTargetEnabled)
.replyTarget(readReplyTarget)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ static ReplyTarget fromJson(final JsonObject jsonObject) {
return ImmutableReplyTarget.fromJson(jsonObject);
}

/**
* Creates a new reply-target object from the specified JSON object, or return an empty optional if it cannot be
* read.
*
* @param jsonObject a JSON object which provides the data for the reply-target to be created.
* @return a new reply-target which is initialised with the extracted data from {@code jsonObject}.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} is not an appropriate JSON object.
*/
static Optional<ReplyTarget> fromJsonOptional(final JsonObject jsonObject) {
return ImmutableReplyTarget.fromJsonOptional(jsonObject);
}

/**
* Create a new builder for a reply-target.
*
Expand Down

0 comments on commit e8460f0

Please sign in to comment.