Browse Source

NotificationRuleRecipientsConfig refactoring, added dedulication logic so that fields not being dulicated in children

pull/15196/head
dashevchenko 7 months ago
parent
commit
4fd7d7eb61
  1. 54
      application/src/main/java/org/thingsboard/server/config/SwaggerConfiguration.java
  2. 4
      common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/DefaultNotificationRuleRecipientsConfig.java
  3. 4
      common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/EscalatedNotificationRuleRecipientsConfig.java
  4. 10
      common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/NotificationRuleRecipientsConfig.java

54
application/src/main/java/org/thingsboard/server/config/SwaggerConfiguration.java

@ -76,6 +76,7 @@ import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -406,6 +407,10 @@ public class SwaggerConfiguration {
} }
}); });
// Deduplicate allOf child schemas: remove properties that are already defined
// in the referenced parent schema to avoid duplication (e.g. EntityId children)
schemas.values().forEach(schema -> deduplicateAllOfProperties(schema, schemas));
// Fix polymorphic request/response bodies: replace inline oneOf with base type $ref // Fix polymorphic request/response bodies: replace inline oneOf with base type $ref
paths.values().stream() paths.values().stream()
.flatMap(pathItem -> pathItem.readOperationsMap().values().stream()) .flatMap(pathItem -> pathItem.readOperationsMap().values().stream())
@ -676,6 +681,55 @@ public class SwaggerConfiguration {
return new ApiResponse().description(description).content(content); return new ApiResponse().description(description).content(content);
} }
@SuppressWarnings("unchecked")
private void deduplicateAllOfProperties(Schema<?> schema, Map<String, Schema> allSchemas) {
if (schema.getAllOf() == null) {
return;
}
// Collect properties defined in any $ref'd parent within the allOf
Set<String> parentProperties = new LinkedHashSet<>();
for (Schema<?> allOfElement : schema.getAllOf()) {
String ref = allOfElement.get$ref();
if (ref != null) {
String refName = ref.substring(ref.lastIndexOf('/') + 1);
Schema<?> parent = allSchemas.get(refName);
if (parent != null && parent.getProperties() != null) {
parentProperties.addAll(parent.getProperties().keySet());
}
}
}
if (parentProperties.isEmpty()) {
return;
}
// Strip those properties from inline (non-$ref) allOf elements
schema.getAllOf().removeIf(allOfElement -> {
if (allOfElement.get$ref() != null) {
return false;
}
if (allOfElement.getProperties() != null) {
parentProperties.forEach(prop -> allOfElement.getProperties().remove(prop));
if (allOfElement.getProperties().isEmpty()) {
allOfElement.setProperties(null);
}
}
// Remove the inline element entirely if it has nothing left
return allOfElement.getProperties() == null
&& allOfElement.getRequired() == null
&& allOfElement.getType() == null;
});
// Remove required entries at the schema level that are already required by the parent
if (schema.getRequired() != null) {
schema.getRequired().removeAll(parentProperties);
if (schema.getRequired().isEmpty()) {
schema.setRequired(null);
}
}
}
private static List<String> resolvePropertyOrder(Class<?> cls, com.fasterxml.jackson.databind.BeanDescription beanDesc) { private static List<String> resolvePropertyOrder(Class<?> cls, com.fasterxml.jackson.databind.BeanDescription beanDesc) {
// Map backing field names to their JSON property names (respects @JsonProperty) // Map backing field names to their JSON property names (respects @JsonProperty)
Map<String, String> fieldToJsonName = new LinkedHashMap<>(); Map<String, String> fieldToJsonName = new LinkedHashMap<>();

4
common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/DefaultNotificationRuleRecipientsConfig.java

@ -27,8 +27,8 @@ import java.util.UUID;
@Schema(description = "Default notification rule recipients configuration", allOf = NotificationRuleRecipientsConfig.class) @Schema(description = "Default notification rule recipients configuration", allOf = NotificationRuleRecipientsConfig.class)
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode
public abstract class DefaultNotificationRuleRecipientsConfig extends NotificationRuleRecipientsConfig { public abstract class DefaultNotificationRuleRecipientsConfig implements NotificationRuleRecipientsConfig {
@NotEmpty @NotEmpty
private List<UUID> targets; private List<UUID> targets;

4
common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/EscalatedNotificationRuleRecipientsConfig.java

@ -27,8 +27,8 @@ import java.util.UUID;
@Schema(description = "Escalated notification rule recipients configuration") @Schema(description = "Escalated notification rule recipients configuration")
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode
public class EscalatedNotificationRuleRecipientsConfig extends NotificationRuleRecipientsConfig { public class EscalatedNotificationRuleRecipientsConfig implements NotificationRuleRecipientsConfig {
@NotEmpty @NotEmpty
private Map<Integer, List<UUID>> escalationTable; private Map<Integer, List<UUID>> escalationTable;

10
common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/NotificationRuleRecipientsConfig.java

@ -16,13 +16,11 @@
package org.thingsboard.server.common.data.notification.rule; package org.thingsboard.server.common.data.notification.rule;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type; import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.swagger.v3.oas.annotations.media.DiscriminatorMapping; import io.swagger.v3.oas.annotations.media.DiscriminatorMapping;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.thingsboard.server.common.data.notification.rule.trigger.config.NotificationRuleTriggerType; import org.thingsboard.server.common.data.notification.rule.trigger.config.NotificationRuleTriggerType;
import java.io.Serializable; import java.io.Serializable;
@ -48,7 +46,6 @@ import java.util.UUID;
@DiscriminatorMapping(value = "TASK_PROCESSING_FAILURE", schema = DefaultNotificationRuleRecipientsConfig.TaskProcessingFailureRecipientsConfig.class), @DiscriminatorMapping(value = "TASK_PROCESSING_FAILURE", schema = DefaultNotificationRuleRecipientsConfig.TaskProcessingFailureRecipientsConfig.class),
@DiscriminatorMapping(value = "RESOURCES_SHORTAGE", schema = DefaultNotificationRuleRecipientsConfig.ResourceShortageRecipientsConfig.class) @DiscriminatorMapping(value = "RESOURCES_SHORTAGE", schema = DefaultNotificationRuleRecipientsConfig.ResourceShortageRecipientsConfig.class)
}) })
@JsonIgnoreProperties
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "triggerType", include = JsonTypeInfo.As.EXISTING_PROPERTY) @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "triggerType", include = JsonTypeInfo.As.EXISTING_PROPERTY)
@JsonSubTypes({ @JsonSubTypes({
@Type(name = "ALARM", value = EscalatedNotificationRuleRecipientsConfig.class), @Type(name = "ALARM", value = EscalatedNotificationRuleRecipientsConfig.class),
@ -66,12 +63,11 @@ import java.util.UUID;
@Type(name = "TASK_PROCESSING_FAILURE", value = DefaultNotificationRuleRecipientsConfig.TaskProcessingFailureRecipientsConfig.class), @Type(name = "TASK_PROCESSING_FAILURE", value = DefaultNotificationRuleRecipientsConfig.TaskProcessingFailureRecipientsConfig.class),
@Type(name = "RESOURCES_SHORTAGE", value = DefaultNotificationRuleRecipientsConfig.ResourceShortageRecipientsConfig.class) @Type(name = "RESOURCES_SHORTAGE", value = DefaultNotificationRuleRecipientsConfig.ResourceShortageRecipientsConfig.class)
}) })
@Data public interface NotificationRuleRecipientsConfig extends Serializable {
public abstract class NotificationRuleRecipientsConfig implements Serializable {
public abstract NotificationRuleTriggerType getTriggerType(); NotificationRuleTriggerType getTriggerType();
@JsonIgnore @JsonIgnore
public abstract Map<Integer, List<UUID>> getTargetsTable(); Map<Integer, List<UUID>> getTargetsTable();
} }

Loading…
Cancel
Save