Skip to content

Commit

Permalink
feat: allow publishing of empty message
Browse files Browse the repository at this point in the history
  • Loading branch information
timonback committed Apr 26, 2024
1 parent baa8627 commit fc47267
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public ResponseEntity<String> publish(@RequestParam String topic, @RequestBody M
}

PublishingPayloadCreator.Result result = publishingPayloadCreator.createPayloadObject(message);
if (result.payload() != null) {
if (result.errorMessage().isEmpty()) {
publishMessage(topic, message, result.payload());
return ResponseEntity.ok().build();
}
return ResponseEntity.badRequest().body(result.errorMessage());
return ResponseEntity.badRequest().body(result.errorMessage().get());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.springwolf.core.asyncapi.components.ComponentsService;
import io.github.springwolf.core.controller.dtos.MessageDto;
import jakarta.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.util.Optional;
import java.util.Set;

/**
Expand All @@ -26,19 +28,23 @@ public class PublishingPayloadCreator {
public Result createPayloadObject(MessageDto message) {
String messagePayloadType = message.getPayloadType();

if (MessageDto.EMPTY.equals(message.getPayload())) {
return new Result(null, Optional.empty());
}

Set<String> knownSchemaNames = componentsService.getSchemas().keySet();
for (String schemaPayloadType : knownSchemaNames) {
// security: match against user input, but always use our controlled data from the DefaultSchemaService
if (schemaPayloadType != null && schemaPayloadType.equals(messagePayloadType)) {
try {
Class<?> payloadClass = Class.forName(schemaPayloadType);
Object payload = objectMapper.readValue(message.getPayload(), payloadClass);
return new Result(payload, null);
return new Result(payload, Optional.empty());
} catch (ClassNotFoundException | JsonProcessingException ex) {
String errorMessage = MessageFormat.format(
"Unable to create payload {0} from data: {1}", schemaPayloadType, message.getPayload());
log.info(errorMessage, ex);
return new Result(null, errorMessage);
return new Result(null, Optional.of(errorMessage));
}
}
}
Expand All @@ -47,9 +53,9 @@ public Result createPayloadObject(MessageDto message) {
"Specified payloadType {0} is not a registered springwolf schema.", messagePayloadType);
String knownPayloadsMessage =
MessageFormat.format(" Known payloadTypes: [{0}]", StringUtils.join(knownSchemaNames, ", "));
log.info(errorMessage + knownPayloadsMessage);
return new Result(null, errorMessage);
log.info("{}{}", errorMessage, knownPayloadsMessage);
return new Result(null, Optional.of(errorMessage));
}

public record Result(Object payload, String errorMessage) {}
public record Result(@Nullable Object payload, Optional<String> errorMessage) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
@Builder
@Jacksonized
public class MessageDto {
public static final String EMPTY = "";

private final Map<String, String> bindings;

private final Map<String, String> headers;

@Builder.Default
private final String payload = "";
private final String payload = EMPTY;

@Builder.Default
private final String payloadType = String.class.getCanonicalName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ export class ChannelMainComponent implements OnInit {
bindings?: string
): void {
try {
const headersJson = JSON.parse(headers);
const bindingsJson = JSON.parse(bindings);
const headersJson = headers != "" ? JSON.parse(headers) : {};
const bindingsJson = bindings != "" ? JSON.parse(bindings) : {};

this.publisherService
.publish(
Expand All @@ -136,9 +136,13 @@ export class ChannelMainComponent implements OnInit {
(err) => this.handlePublishError(err)
);
} catch (error) {
this.snackBar.open("Example payload is not valid", "ERROR", {
duration: 3000,
});
this.snackBar.open(
"Example payload is not valid " + JSON.stringify(error),
"ERROR",
{
duration: 3000,
}
);
}
}

Expand Down
3 changes: 3 additions & 0 deletions springwolf-ui/src/app/models/example.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export class Example {
this.value = exampleObject;
} else {
this.value = JSON.stringify(exampleObject, null, 2);
if (this.value === "{}") {
this.value = "";
}
}

this.lineCount = this.value.split("\n").length;
Expand Down

0 comments on commit fc47267

Please sign in to comment.