Skip to content

Commit b347bcc

Browse files
authored
Merge pull request #2276 from beyonnex-io/bugfix/duplicate-wot-submodel-exception-fix
fix wrong exceptions being thrown when duplicates occur in WoT parsing
2 parents 153b8f2 + ca21123 commit b347bcc

File tree

13 files changed

+49
-19
lines changed

13 files changed

+49
-19
lines changed

wot/api/src/main/java/org/eclipse/ditto/wot/api/resolver/DefaultWotThingModelResolver.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.net.URL;
1717
import java.time.Duration;
1818
import java.util.AbstractMap;
19+
import java.util.LinkedHashMap;
1920
import java.util.List;
2021
import java.util.Map;
2122
import java.util.concurrent.CompletableFuture;
@@ -130,7 +131,11 @@ public CompletionStage<Map<ThingSubmodel, ThingModel>> resolveThingModelSubmodel
130131
return CompletableFuture.allOf(futureList.toArray(CompletableFuture<?>[]::new))
131132
.thenApplyAsync(aVoid -> futureList.stream()
132133
.map(CompletableFuture::join) // joining does not block anything here as "allOf" already guaranteed that all futures are ready
133-
.collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue))
134+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (u, v) -> {
135+
throw WotThingModelInvalidException.newBuilder(String.format("Thing submodels: Duplicate key %s", u))
136+
.dittoHeaders(dittoHeaders)
137+
.build();
138+
}, LinkedHashMap::new))
134139
);
135140
}
136141

wot/api/src/main/java/org/eclipse/ditto/wot/api/validator/DefaultWotThingModelValidator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.eclipse.ditto.wot.api.resolver.ThingSubmodel;
5454
import org.eclipse.ditto.wot.api.resolver.WotThingModelResolver;
5555
import org.eclipse.ditto.wot.model.ThingModel;
56+
import org.eclipse.ditto.wot.model.WotThingModelInvalidException;
5657
import org.eclipse.ditto.wot.validation.JsonSchemaCacheKey;
5758
import org.eclipse.ditto.wot.validation.ValidationContext;
5859
import org.eclipse.ditto.wot.validation.WotThingModelPayloadValidationException;
@@ -991,7 +992,11 @@ private CompletionStage<Void> doValidateMergeThingAffectedFeatures(final Thing t
991992
.filter(subModel ->
992993
affectedFeatures.contains(subModel.getKey().instanceName())
993994
)
994-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
995+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (u, v) -> {
996+
throw WotThingModelInvalidException.newBuilder(String.format("Thing submodels: Duplicate key %s", u))
997+
.dittoHeaders(context.dittoHeaders())
998+
.build();
999+
}, LinkedHashMap::new));
9951000
final Features filteredFeatures = thing.getFeatures()
9961001
.map(features -> Features.newBuilder()
9971002
.setAll(features.stream()

wot/model/src/main/java/org/eclipse/ditto/wot/model/Actions.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ static Actions fromJson(final JsonObject jsonObject) {
3434
field -> field.getKey().toString(),
3535
field -> Action.fromJson(field.getKey().toString(), field.getValue().asObject()),
3636
(u, v) -> {
37-
throw new IllegalStateException(String.format("Duplicate key %s", u));
37+
throw WotThingModelInvalidException.newBuilder(String.format("Actions: Duplicate key %s", u))
38+
.build();
3839
},
3940
LinkedHashMap::new)
4041
));
@@ -45,7 +46,8 @@ static Actions from(final Collection<Action> actions) {
4546
Action::getActionName,
4647
a -> a,
4748
(u, v) -> {
48-
throw new IllegalStateException(String.format("Duplicate key %s", u));
49+
throw WotThingModelInvalidException.newBuilder(String.format("Actions: Duplicate key %s", u))
50+
.build();
4951
},
5052
LinkedHashMap::new)
5153
));

wot/model/src/main/java/org/eclipse/ditto/wot/model/Descriptions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ static Descriptions fromJson(final JsonObject jsonObject) {
3333
field -> new Locale(field.getKey().toString()),
3434
field -> Description.of(field.getValue().asString()),
3535
(u, v) -> {
36-
throw new IllegalStateException(String.format("Duplicate key %s", u));
36+
throw WotThingModelInvalidException.newBuilder(String.format("Descriptions: Duplicate key %s", u))
37+
.build();
3738
},
3839
LinkedHashMap::new)
3940
));

wot/model/src/main/java/org/eclipse/ditto/wot/model/Events.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ static Events fromJson(final JsonObject jsonObject) {
3434
field -> field.getKey().toString(),
3535
field -> Event.fromJson(field.getKey().toString(), field.getValue().asObject()),
3636
(u, v) -> {
37-
throw new IllegalStateException(String.format("Duplicate key %s", u));
37+
throw WotThingModelInvalidException.newBuilder(String.format("Events: Duplicate key %s", u))
38+
.build();
3839
},
3940
LinkedHashMap::new)
4041
));
@@ -45,7 +46,8 @@ static Events from(final Collection<Event> events) {
4546
Event::getEventName,
4647
e -> e,
4748
(u, v) -> {
48-
throw new IllegalStateException(String.format("Duplicate key %s", u));
49+
throw WotThingModelInvalidException.newBuilder(String.format("Events: Duplicate key %s", u))
50+
.build();
4951
},
5052
LinkedHashMap::new)
5153
));

wot/model/src/main/java/org/eclipse/ditto/wot/model/ImmutableObjectSchema.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public Map<String, SingleDataSchema> getProperties() {
5252
f -> f.getKey().toString(),
5353
f -> SingleDataSchema.fromJson(f.getValue().asObject()),
5454
(u, v) -> {
55-
throw new IllegalStateException(String.format("Duplicate key %s", u));
55+
throw WotThingModelInvalidException.newBuilder(String.format("Object properties: Duplicate key %s", u))
56+
.build();
5657
},
5758
LinkedHashMap::new
5859
));

wot/model/src/main/java/org/eclipse/ditto/wot/model/Properties.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ static Properties fromJson(final JsonObject jsonObject) {
3434
field -> field.getKey().toString(),
3535
field -> Property.fromJson(field.getKey().toString(), field.getValue().asObject()),
3636
(u, v) -> {
37-
throw new IllegalStateException(String.format("Duplicate key %s", u));
37+
throw WotThingModelInvalidException.newBuilder(String.format("Properties: Duplicate key %s", u))
38+
.build();
3839
},
3940
LinkedHashMap::new)
4041
));
@@ -45,7 +46,8 @@ static Properties from(final Collection<Property> properties) {
4546
Property::getPropertyName,
4647
p -> p,
4748
(u, v) -> {
48-
throw new IllegalStateException(String.format("Duplicate key %s", u));
49+
throw WotThingModelInvalidException.newBuilder(String.format("Properties: Duplicate key %s", u))
50+
.build();
4951
},
5052
LinkedHashMap::new)
5153
));

wot/model/src/main/java/org/eclipse/ditto/wot/model/SchemaDefinitions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ static SchemaDefinitions fromJson(final JsonObject jsonObject) {
3333
field -> field.getKey().toString(),
3434
field -> SingleDataSchema.fromJson(field.getValue().asObject()),
3535
(u, v) -> {
36-
throw new IllegalStateException(String.format("Duplicate key %s", u));
36+
throw WotThingModelInvalidException.newBuilder(String.format("SingleDataSchemas: Duplicate key %s", u))
37+
.build();
3738
},
3839
LinkedHashMap::new)
3940
));

wot/model/src/main/java/org/eclipse/ditto/wot/model/SecurityDefinitions.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ static SecurityDefinitions fromJson(final JsonObject jsonObject) {
3333
field -> field.getKey().toString(),
3434
field -> SecurityScheme.fromJson(field.getKey().toString(), field.getValue().asObject()),
3535
(u, v) -> {
36-
throw new IllegalStateException(String.format("Duplicate key %s", u));
36+
throw WotThingModelInvalidException.newBuilder(String.format("SecuritySchemes: Duplicate key %s", u))
37+
.build();
3738
},
3839
LinkedHashMap::new)
3940
));
@@ -44,7 +45,8 @@ static SecurityDefinitions from(final Collection<SecurityScheme> securityDefinit
4445
SecurityScheme::getSecuritySchemeName,
4546
s -> s,
4647
(u, v) -> {
47-
throw new IllegalStateException(String.format("Duplicate key %s", u));
48+
throw WotThingModelInvalidException.newBuilder(String.format("SecuritySchemes: Duplicate key %s", u))
49+
.build();
4850
},
4951
LinkedHashMap::new)
5052
));

wot/model/src/main/java/org/eclipse/ditto/wot/model/Titles.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ static Titles fromJson(final JsonObject jsonObject) {
3434
field -> new Locale(field.getKey().toString()),
3535
field -> Title.of(field.getValue().asString()),
3636
(u, v) -> {
37-
throw new IllegalStateException(String.format("Duplicate key %s", u));
37+
throw WotThingModelInvalidException.newBuilder(String.format("Titles: Duplicate key %s", u))
38+
.build();
3839
},
3940
LinkedHashMap::new)
4041
));

0 commit comments

Comments
 (0)