Skip to content

Commit

Permalink
Feature/28656 fix southbound findings (#692)
Browse files Browse the repository at this point in the history
* allow conversion of integer to floats

* prevent 500 on tag conversion error

* drop publish in case of inaccurate int > float casting

* change datetime from long to string

* fix trailing empty space

* fix after rebase

* fix double parsing

* fix double schema
  • Loading branch information
DC2-DanielKrueger authored Dec 12, 2024
1 parent eb5dc42 commit 5417a21
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.hivemq.adapter.sdk.api.writing.WritingProtocolAdapter;
import com.hivemq.api.AbstractApi;
import com.hivemq.api.adapters.AdapterConfigModel;
import com.hivemq.api.errors.InvalidInputError;
import com.hivemq.api.format.DataUrl;
import com.hivemq.api.errors.AlreadyExistsError;
import com.hivemq.api.errors.BadRequestError;
import com.hivemq.api.errors.InternalServerError;
Expand Down Expand Up @@ -672,7 +674,15 @@ protected void validateAdapterSchema(
.map(DomainTag::toTagMap)
.collect(Collectors.toList());

final List<? extends Tag> tags = configConverter.mapsToTags(adapterType, domainTags);
final List<? extends Tag> tags;
try {
tags = configConverter.mapsToTags(adapterType, domainTags);
} catch (final IllegalArgumentException illegalArgumentException) {
log.warn("Unable to parse tags for adapter '{}'", adapterName);
log.debug("Original Exception: ", illegalArgumentException);
return ErrorResponseUtil.errorResponse(new InvalidInputError(
"Exception during parsing of tags for the adapter. See log for further information."));
}

final List<NorthboundMapping> northboundMappings = adapter.getNorthboundMappingModels()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ public BuiltinJsonSchema() {
classToJsonSchema.put(BuiltinDataType.Boolean,
createJsonSchemaForBuiltinType("Boolean JsonSchema", BuiltinDataType.Boolean));

classToJsonSchema.put(BuiltinDataType.SByte, createJsonSchemaForBuiltinType(" SByte JsonSchema", BuiltinDataType.SByte));
classToJsonSchema.put(BuiltinDataType.Byte, createJsonSchemaForBuiltinType(" Byte JsonSchema", BuiltinDataType.Byte));
classToJsonSchema.put(BuiltinDataType.SByte,
createJsonSchemaForBuiltinType("SByte JsonSchema", BuiltinDataType.SByte));
classToJsonSchema.put(BuiltinDataType.Byte,
createJsonSchemaForBuiltinType("Byte JsonSchema", BuiltinDataType.Byte));

classToJsonSchema.put(BuiltinDataType.UInt64,
createJsonSchemaForBuiltinType("UInt64 JsonSchema", BuiltinDataType.UInt64));
Expand Down Expand Up @@ -110,8 +112,7 @@ public BuiltinJsonSchema() {
}

private @NotNull JsonNode createJsonSchemaForBuiltinType(
final @NotNull String title,
final @NotNull BuiltinDataType builtinDataType) {
final @NotNull String title, final @NotNull BuiltinDataType builtinDataType) {
final ObjectNode rootNode = OBJECT_MAPPER.createObjectNode();
final ObjectNode propertiesNode = OBJECT_MAPPER.createObjectNode();
final ObjectNode valueNode = OBJECT_MAPPER.createObjectNode();
Expand Down Expand Up @@ -156,6 +157,10 @@ public static void populatePropertiesForBuiltinType(
case LocalizedText:
nestedPropertiesNode.set("type", new TextNode("string"));
return;
case DateTime:
nestedPropertiesNode.set("type", new TextNode("string"));
nestedPropertiesNode.set("format", new TextNode("date-time"));
return;
case Int16:
nestedPropertiesNode.set("type", new TextNode(INTEGER_DATA_TYPE));
nestedPropertiesNode.set(MINIMUM_KEY_WORD, new ShortNode(Short.MIN_VALUE));
Expand All @@ -177,7 +182,6 @@ public static void populatePropertiesForBuiltinType(
nestedPropertiesNode.set(MINIMUM_KEY_WORD, new LongNode(UInteger.MIN_VALUE));
nestedPropertiesNode.set(MAXIMUM_KEY_WORD, new LongNode(UInteger.MAX_VALUE));
return;
case DateTime:
case Int64:
nestedPropertiesNode.set("type", new TextNode(INTEGER_DATA_TYPE));
nestedPropertiesNode.set(MINIMUM_KEY_WORD, new LongNode(Long.MIN_VALUE));
Expand All @@ -195,7 +199,7 @@ public static void populatePropertiesForBuiltinType(
return;
case Double:
nestedPropertiesNode.set("type", new TextNode("number"));
nestedPropertiesNode.set(MINIMUM_KEY_WORD, new DoubleNode(Double.MIN_VALUE));
nestedPropertiesNode.set(MINIMUM_KEY_WORD, new DoubleNode(-Double.MAX_VALUE));
nestedPropertiesNode.set(MAXIMUM_KEY_WORD, new DoubleNode(Double.MAX_VALUE));
return;
case QualifiedName:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.time.Instant;
import java.util.Date;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -359,8 +361,8 @@ private static UUID extractGuid(final JsonNode jsonNode) {
}

private static DateTime extractDateTime(final JsonNode jsonNode) {
if (jsonNode.isLong()) {
return new DateTime(jsonNode.asLong());
if (jsonNode.isTextual()) {
return new DateTime(Date.from(Instant.parse(jsonNode.asText())));
}
throw createException(jsonNode, BuiltinDataType.DateTime.name());
}
Expand All @@ -372,10 +374,24 @@ private static DateTime extractDateTime(final JsonNode jsonNode) {
throw createException(jsonNode, BuiltinDataType.String.name());
}


static double extractDouble(final JsonNode jsonNode) {
if (jsonNode.isDouble()) {
return jsonNode.asDouble();
}

if (jsonNode.isInt()) {
final double parsedDouble = jsonNode.intValue();
final int parsedIntegerBack = (int) parsedDouble;
if (parsedIntegerBack != jsonNode.intValue()) {
throw new IllegalArgumentException(String.format(
"An integer was supplied for a double node that is not representable without rounding. Input Integer: '%d', Output Double: '%f'. To avoid inaccuracies the publish will not be consumed.",
jsonNode.intValue(),
parsedDouble));
}
return parsedDouble;
}

throw createException(jsonNode, BuiltinDataType.Double.name());
}

Expand All @@ -387,6 +403,19 @@ static float extractFloat(final JsonNode jsonNode) {
if (jsonNode.isFloat()) {
return jsonNode.floatValue();
}

if (jsonNode.isInt()) {
final float parsedFloat = jsonNode.intValue();
final int parsedIntegerBack = (int) parsedFloat;
if (parsedIntegerBack != jsonNode.intValue()) {
throw new IllegalArgumentException(String.format(
"An integer was supplied for a float node that is not representable without rounding. Input Integer: '%d', Output Float: '%f'. To avoid inaccuracies the publish will not be consumed.",
jsonNode.intValue(),
parsedFloat));
}
return parsedFloat;
}

throw createException(jsonNode, BuiltinDataType.Float.name());
}

Expand Down

0 comments on commit 5417a21

Please sign in to comment.