Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
future.complete(new ManagementApiHttpResponse<>(
Stream.fromSse(
Stream.fromSseWithEventDiscrimination(
EventStreamSubscribeEventsResponseContent.class,
new ResponseBodyReader(response)),
new ResponseBodyReader(response),
"type"),
response));
return;
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/auth0/client/mgmt/RawEventsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ public ManagementApiHttpResponse<Iterable<EventStreamSubscribeEventsResponseCont
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
return new ManagementApiHttpResponse<>(
Stream.fromSse(
EventStreamSubscribeEventsResponseContent.class, new ResponseBodyReader(response)),
Stream.fromSseWithEventDiscrimination(
EventStreamSubscribeEventsResponseContent.class,
new ResponseBodyReader(response),
"type"),
response);
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/auth0/client/mgmt/core/SseEventParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,44 @@ public static <T> T parseEventLevelUnion(
}
}

/**
* Parse an SSE event whose discriminator is not part of the SSE envelope shape (i.e.
* {@link #isEventLevelDiscrimination(String)} is {@code false}) but is also absent from the
* wire payload itself. Merges the SSE {@code event:} name into the root of the parsed
* {@code data:} object under {@code discriminatorProperty}, without overwriting a
* pre-existing value, then deserializes the merged object to the target type.
*
* @param eventType The SSE event type (from event: field)
* @param data The SSE data content (from data: field)
* @param unionClass The target union class
* @param discriminatorProperty The property name used for discrimination (e.g., "type")
* @param <T> The target type
* @return The deserialized object
*/
public static <T> T parseWithInjectedDiscriminator(
String eventType, String data, Class<T> unionClass, String discriminatorProperty) {
Map<String, Object> parsedData = null;
if (data != null && !data.isEmpty()) {
try {
parsedData = ObjectMappers.JSON_MAPPER.readValue(data, new TypeReference<Map<String, Object>>() {});
} catch (Exception e) {
parsedData = null;
}
}
try {
if (parsedData == null) {
return parseDataLevelUnion(data, unionClass);
}
if (eventType != null) {
parsedData.putIfAbsent(discriminatorProperty, eventType);
}
String mergedJson = ObjectMappers.JSON_MAPPER.writeValueAsString(parsedData);
return ObjectMappers.JSON_MAPPER.readValue(mergedJson, unionClass);
} catch (Exception e) {
throw new RuntimeException("Failed to parse SSE event with injected discriminator", e);
}
}

/**
* Parse an SSE event using data-level discrimination.
* <p>
Expand Down
27 changes: 12 additions & 15 deletions src/main/java/com/auth0/client/mgmt/core/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,7 @@ private boolean readNextMessage() {
if (line.trim().isEmpty()) {
if (eventDataBuffer.length() > 0 || currentEventType != null) {
try {
// Use SseEventParser for event-level discrimination
nextItem = SseEventParser.parseEventLevelUnion(
currentEventType,
eventDataBuffer.toString(),
currentEventId,
currentRetry,
valueType,
discriminatorProperty);
nextItem = parseCurrentEvent();
hasNextItem = true;
resetEventState();
return true;
Expand Down Expand Up @@ -477,13 +470,7 @@ private boolean readNextMessage() {
// Handle any remaining buffered data at end of stream
if (eventDataBuffer.length() > 0 || currentEventType != null) {
try {
nextItem = SseEventParser.parseEventLevelUnion(
currentEventType,
eventDataBuffer.toString(),
currentEventId,
currentRetry,
valueType,
discriminatorProperty);
nextItem = parseCurrentEvent();
hasNextItem = true;
resetEventState();
return true;
Expand All @@ -509,5 +496,15 @@ private void resetEventState() {
currentEventId = null;
currentRetry = null;
}

private T parseCurrentEvent() {
String data = eventDataBuffer.toString();
if (SseEventParser.isEventLevelDiscrimination(discriminatorProperty)) {
return SseEventParser.parseEventLevelUnion(
currentEventType, data, currentEventId, currentRetry, valueType, discriminatorProperty);
}
return SseEventParser.parseWithInjectedDiscriminator(
currentEventType, data, valueType, discriminatorProperty);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,58 +113,56 @@ static final class Deserializer extends StdDeserializer<EventStreamCloudEventUse
public EventStreamCloudEventUserCreatedObjectIdentitiesItem deserialize(
JsonParser p, DeserializationContext context) throws IOException {
Object value = p.readValueAs(Object.class);
if (value instanceof Map<?, ?>
&& ((Map<?, ?>) value).containsKey("connection")
&& ((Map<?, ?>) value).containsKey("user_id")
&& ((Map<?, ?>) value).containsKey("provider")
&& ((Map<?, ?>) value).containsKey("isSocial")) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserCreatedObjectIdentitiesItemCustom.class));
} catch (RuntimeException e) {
if (value instanceof Map<?, ?>) {
Map<?, ?> map = (Map<?, ?>) value;
Object providerValue = map.get("provider");
String provider = providerValue == null ? null : providerValue.toString();
boolean isSocial = Boolean.TRUE.equals(map.get("isSocial"));

if (isSocial) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserCreatedObjectIdentitiesItemSocial.class));
} catch (RuntimeException e) {
}
}
}
if (value instanceof Map<?, ?>
&& ((Map<?, ?>) value).containsKey("connection")
&& ((Map<?, ?>) value).containsKey("user_id")
&& ((Map<?, ?>) value).containsKey("provider")
&& ((Map<?, ?>) value).containsKey("isSocial")) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserCreatedObjectIdentitiesItemDatabase.class));
} catch (RuntimeException e) {
if (provider != null
&& EventStreamCloudEventUserCreatedObjectIdentitiesItemDatabaseProviderEnum.valueOf(provider)
.getEnumValue()
!= EventStreamCloudEventUserCreatedObjectIdentitiesItemDatabaseProviderEnum.Value
.UNKNOWN) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserCreatedObjectIdentitiesItemDatabase.class));
} catch (RuntimeException e) {
}
}
}
if (value instanceof Map<?, ?>
&& ((Map<?, ?>) value).containsKey("connection")
&& ((Map<?, ?>) value).containsKey("user_id")
&& ((Map<?, ?>) value).containsKey("provider")
&& ((Map<?, ?>) value).containsKey("isSocial")) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserCreatedObjectIdentitiesItemEnterprise.class));
} catch (RuntimeException e) {
if (provider != null
&& EventStreamCloudEventUserCreatedObjectIdentitiesItemPasswordlessProviderEnum.valueOf(
provider)
.getEnumValue()
!= EventStreamCloudEventUserCreatedObjectIdentitiesItemPasswordlessProviderEnum.Value
.UNKNOWN) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserCreatedObjectIdentitiesItemPasswordless.class));
} catch (RuntimeException e) {
}
}
}
if (value instanceof Map<?, ?>
&& ((Map<?, ?>) value).containsKey("connection")
&& ((Map<?, ?>) value).containsKey("user_id")
&& ((Map<?, ?>) value).containsKey("provider")
&& ((Map<?, ?>) value).containsKey("isSocial")) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserCreatedObjectIdentitiesItemPasswordless.class));
} catch (RuntimeException e) {
if (provider != null
&& EventStreamCloudEventUserCreatedObjectIdentitiesItemEnterpriseProviderEnum.valueOf(provider)
.getEnumValue()
!= EventStreamCloudEventUserCreatedObjectIdentitiesItemEnterpriseProviderEnum.Value
.UNKNOWN) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserCreatedObjectIdentitiesItemEnterprise.class));
} catch (RuntimeException e) {
}
}
}
if (value instanceof Map<?, ?>
&& ((Map<?, ?>) value).containsKey("connection")
&& ((Map<?, ?>) value).containsKey("user_id")
&& ((Map<?, ?>) value).containsKey("provider")
&& ((Map<?, ?>) value).containsKey("isSocial")) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserCreatedObjectIdentitiesItemSocial.class));
value, EventStreamCloudEventUserCreatedObjectIdentitiesItemCustom.class));
} catch (RuntimeException e) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,58 +113,56 @@ static final class Deserializer extends StdDeserializer<EventStreamCloudEventUse
public EventStreamCloudEventUserDeletedObjectIdentitiesItem deserialize(
JsonParser p, DeserializationContext context) throws IOException {
Object value = p.readValueAs(Object.class);
if (value instanceof Map<?, ?>
&& ((Map<?, ?>) value).containsKey("connection")
&& ((Map<?, ?>) value).containsKey("user_id")
&& ((Map<?, ?>) value).containsKey("provider")
&& ((Map<?, ?>) value).containsKey("isSocial")) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserDeletedObjectIdentitiesItemCustom.class));
} catch (RuntimeException e) {
if (value instanceof Map<?, ?>) {
Map<?, ?> map = (Map<?, ?>) value;
Object providerValue = map.get("provider");
String provider = providerValue == null ? null : providerValue.toString();
boolean isSocial = Boolean.TRUE.equals(map.get("isSocial"));

if (isSocial) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserDeletedObjectIdentitiesItemSocial.class));
} catch (RuntimeException e) {
}
}
}
if (value instanceof Map<?, ?>
&& ((Map<?, ?>) value).containsKey("connection")
&& ((Map<?, ?>) value).containsKey("user_id")
&& ((Map<?, ?>) value).containsKey("provider")
&& ((Map<?, ?>) value).containsKey("isSocial")) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserDeletedObjectIdentitiesItemDatabase.class));
} catch (RuntimeException e) {
if (provider != null
&& EventStreamCloudEventUserDeletedObjectIdentitiesItemDatabaseProviderEnum.valueOf(provider)
.getEnumValue()
!= EventStreamCloudEventUserDeletedObjectIdentitiesItemDatabaseProviderEnum.Value
.UNKNOWN) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserDeletedObjectIdentitiesItemDatabase.class));
} catch (RuntimeException e) {
}
}
}
if (value instanceof Map<?, ?>
&& ((Map<?, ?>) value).containsKey("connection")
&& ((Map<?, ?>) value).containsKey("user_id")
&& ((Map<?, ?>) value).containsKey("provider")
&& ((Map<?, ?>) value).containsKey("isSocial")) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserDeletedObjectIdentitiesItemEnterprise.class));
} catch (RuntimeException e) {
if (provider != null
&& EventStreamCloudEventUserDeletedObjectIdentitiesItemPasswordlessProviderEnum.valueOf(
provider)
.getEnumValue()
!= EventStreamCloudEventUserDeletedObjectIdentitiesItemPasswordlessProviderEnum.Value
.UNKNOWN) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserDeletedObjectIdentitiesItemPasswordless.class));
} catch (RuntimeException e) {
}
}
}
if (value instanceof Map<?, ?>
&& ((Map<?, ?>) value).containsKey("connection")
&& ((Map<?, ?>) value).containsKey("user_id")
&& ((Map<?, ?>) value).containsKey("provider")
&& ((Map<?, ?>) value).containsKey("isSocial")) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserDeletedObjectIdentitiesItemPasswordless.class));
} catch (RuntimeException e) {
if (provider != null
&& EventStreamCloudEventUserDeletedObjectIdentitiesItemEnterpriseProviderEnum.valueOf(provider)
.getEnumValue()
!= EventStreamCloudEventUserDeletedObjectIdentitiesItemEnterpriseProviderEnum.Value
.UNKNOWN) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserDeletedObjectIdentitiesItemEnterprise.class));
} catch (RuntimeException e) {
}
}
}
if (value instanceof Map<?, ?>
&& ((Map<?, ?>) value).containsKey("connection")
&& ((Map<?, ?>) value).containsKey("user_id")
&& ((Map<?, ?>) value).containsKey("provider")
&& ((Map<?, ?>) value).containsKey("isSocial")) {
try {
return of(ObjectMappers.JSON_MAPPER.convertValue(
value, EventStreamCloudEventUserDeletedObjectIdentitiesItemSocial.class));
value, EventStreamCloudEventUserDeletedObjectIdentitiesItemCustom.class));
} catch (RuntimeException e) {
}
}
Expand Down
Loading