Skip to content

Commit

Permalink
refactor(core): update AsyncApiService default impl
Browse files Browse the repository at this point in the history
  • Loading branch information
timonback committed Nov 1, 2024
1 parent 096fbb9 commit 34117a3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ public interface AsyncApiService {

AsyncAPI getAsyncAPI();

// TODO where do we want do add the breaking change?
/**
* Default implementation was added to avoid breaking (compiler) change.
*
* Maintainer note: remove default implementation
*/
default Optional<AsyncAPI> getForGroupName(String groupName) {
return Optional.ofNullable(getAsyncAPI());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import io.github.springwolf.core.asyncapi.operations.OperationsService;
import io.github.springwolf.core.configuration.docket.AsyncApiDocket;
import io.github.springwolf.core.configuration.docket.AsyncApiDocketService;
import jakarta.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -25,13 +25,9 @@ public class DefaultAsyncApiService implements AsyncApiService {

/**
* Record holding the result of AsyncAPI creation.
*
* @param asyncAPI
* @param exception
*/
private record AsyncAPIResult(AsyncAPI asyncAPI, Throwable exception) {}
// -> master (internal)
// -> per group
private record AsyncAPIResult(
@Nullable AsyncAPI asyncAPI, @Nullable Map<String, AsyncAPI> groupedApi, Throwable exception) {}

private final AsyncApiDocketService asyncApiDocketService;
private final ChannelsService channelsService;
Expand All @@ -41,7 +37,6 @@ private record AsyncAPIResult(AsyncAPI asyncAPI, Throwable exception) {}
private final AsyncApiGroupService groupService;

private volatile AsyncAPIResult asyncAPIResult = null;
private volatile Map<String, AsyncAPI> asyncApiGroupMap = new HashMap<>();

@Override
public AsyncAPI getAsyncAPI() {
Expand All @@ -62,8 +57,11 @@ public Optional<AsyncAPI> getForGroupName(String groupName) {
initAsyncAPI();
}

// TODO: should be grouping be part of the AsyncAPIResult class?
return Optional.ofNullable(this.asyncApiGroupMap.get(groupName));
if (asyncAPIResult.asyncAPI != null) {
return Optional.ofNullable(asyncAPIResult.groupedApi.get(groupName));
} else {
throw new RuntimeException("Error occurred during creation of AsyncAPI", asyncAPIResult.exception);
}
}

/**
Expand Down Expand Up @@ -102,19 +100,17 @@ protected synchronized void initAsyncAPI() {
.components(components)
.build();

// master = asyncAPI;

for (AsyncApiCustomizer customizer : customizers) {
log.debug("Starting customizer {}", customizer.getClass().getName());
customizer.customize(asyncAPI);
}
this.asyncAPIResult = new AsyncAPIResult(asyncAPI, null);
this.asyncApiGroupMap = groupService.group(asyncAPI);
Map<String, AsyncAPI> groupedApi = groupService.group(asyncAPI);
this.asyncAPIResult = new AsyncAPIResult(asyncAPI, groupedApi, null);

log.debug("AsyncAPI document was built");
} catch (Throwable t) {
log.debug("Failed to build AsyncAPI document", t);
this.asyncAPIResult = new AsyncAPIResult(null, t);
this.asyncAPIResult = new AsyncAPIResult(null, null, t);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* public void receiveMessage(MonetaryAmount payload) { ... }
* </pre>
*
* Maintainer node: move to io.github.springwolf.core.asyncapi.annotation
* Maintainer note: move to io.github.springwolf.core.asyncapi.annotation
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.springframework.test.context.TestPropertySource;

import java.util.Map;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -312,7 +311,8 @@ void shouldFindAllForGroupAll() {
AsyncAPI fullApi = asyncApiService.getAsyncAPI();

// when
AsyncAPI asyncAPIOpt = asyncApiService.getForGroupName("all & everything").get();
AsyncAPI asyncAPIOpt =
asyncApiService.getForGroupName("all & everything").get();

// then

Expand Down

0 comments on commit 34117a3

Please sign in to comment.