Skip to content

Commit

Permalink
feat(core): add override for info object on grouped api
Browse files Browse the repository at this point in the history
Co-authored-by: Timon Back <[email protected]>
  • Loading branch information
sam0r040 and timonback committed Nov 22, 2024
1 parent 0eaa374 commit b882612
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package io.github.springwolf.core.asyncapi.grouping;

import io.github.springwolf.asyncapi.v3.model.AsyncAPI;
import io.github.springwolf.asyncapi.v3.model.info.Info;
import io.github.springwolf.core.configuration.docket.AsyncApiGroup;
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
import lombok.RequiredArgsConstructor;
Expand All @@ -14,6 +15,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.github.springwolf.core.configuration.docket.DefaultAsyncApiDocketService.mapInfo;

@Slf4j
@RequiredArgsConstructor
public class AsyncApiGroupService {
Expand All @@ -22,7 +25,11 @@ public class AsyncApiGroupService {

public Map<String, AsyncAPI> group(AsyncAPI asyncAPI) {
return getAsyncApiGroups()
.map(group -> Map.entry(group.getGroupName(), groupingService.groupAPI(asyncAPI, group)))
.map(group -> {
AsyncAPI groupedApi = groupingService.groupAPI(asyncAPI, group);
groupedApi.setInfo(merge(groupedApi.getInfo(), group.getGroupInfo()));
return Map.entry(group.getGroupName(), groupedApi);
})
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

Expand All @@ -43,23 +50,39 @@ private static AsyncApiGroup toGroupConfigAndValidate(SpringwolfConfigProperties
}

int allItemCount = group.getActionToMatch().size()
+ group.getChannelNameToMatch().size()
+ group.getMessageNameToMatch().size();
+ group.getChannelNameToMatch().size()
+ group.getMessageNameToMatch().size();
if (allItemCount != 0
&& group.getActionToMatch().size() != allItemCount
&& channelNameToMatch.size() != allItemCount
&& messageNameToMatch.size() != allItemCount) {
&& group.getActionToMatch().size() != allItemCount
&& channelNameToMatch.size() != allItemCount
&& messageNameToMatch.size() != allItemCount) {
throw new IllegalArgumentException(
"AsyncApiGroup %s must specify at most one filter criteria".formatted(groupName));
}

AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder()
.groupName(groupName)
.groupInfo(mapInfo(group.getInfo()))
.operationActionsToKeep(group.getActionToMatch())
.channelNamesToKeep(channelNameToMatch)
.messageNamesToKeep(messageNameToMatch)
.build();
log.debug("Loaded AsyncApiGroup from configuration: {}", asyncApiGroup);
return asyncApiGroup;
}

public static Info merge(Info original, Info updates) {
return Info.builder()
.title(updates.getTitle() != null ? updates.getTitle() : original.getTitle())
.version(updates.getVersion() != null ? updates.getVersion() : original.getVersion())
.description(updates.getDescription() != null ? updates.getDescription() : original.getDescription())
.termsOfService(updates.getTermsOfService() != null ? updates.getTermsOfService() : original.getTermsOfService())
.contact(updates.getContact() != null ? updates.getContact() : original.getContact())
.license(updates.getLicense() != null ? updates.getLicense() : original.getLicense())
.tags(updates.getTags() != null ? updates.getTags() : original.getTags())
.externalDocs(updates.getExternalDocs() != null ? updates.getExternalDocs() : original.getExternalDocs())
// TODO fixme
//.extensionFields(updates.getExtensionFields() != null ? updates.getExtensionFields() : original.getExtensionFields())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import io.github.springwolf.asyncapi.v3.model.channel.ChannelObject;
import io.github.springwolf.asyncapi.v3.model.channel.message.MessageObject;
import io.github.springwolf.asyncapi.v3.model.info.Info;
import io.github.springwolf.asyncapi.v3.model.operation.Operation;
import io.github.springwolf.asyncapi.v3.model.operation.OperationAction;
import lombok.AllArgsConstructor;
Expand All @@ -23,6 +24,8 @@
public class AsyncApiGroup {
private final String groupName;

private final Info groupInfo;

@Builder.Default
private final List<OperationAction> operationActionsToKeep = Collections.emptyList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ private static Info buildInfo(@Nullable SpringwolfConfigProperties.ConfigDocket.
+ " is not set.");
}

Info asyncapiInfo = mapInfo(configDocketInfo);

return asyncapiInfo;
}

public static Info mapInfo(SpringwolfConfigProperties.ConfigDocket.Info configDocketInfo) {
Info asyncapiInfo = Info.builder()
.version(configDocketInfo.getVersion())
.title(configDocketInfo.getTitle())
Expand All @@ -76,7 +82,6 @@ private static Info buildInfo(@Nullable SpringwolfConfigProperties.ConfigDocket.
Map<String, Object> extFieldsMap = Map.copyOf(configDocketInfo.getExtensionFields());
asyncapiInfo.setExtensionFields(extFieldsMap);
}

return asyncapiInfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ public static class Group {
* The message names to match
*/
private List<String> messageNameToMatch = Collections.emptyList();

/**
* Allows to override the info object with group specific information .
*
* @see Info
*/
private Info info;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package io.github.springwolf.core.asyncapi.grouping;

import io.github.springwolf.asyncapi.v3.model.AsyncAPI;
import io.github.springwolf.asyncapi.v3.model.info.Info;
import io.github.springwolf.asyncapi.v3.model.operation.OperationAction;
import io.github.springwolf.core.configuration.docket.AsyncApiGroup;
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
Expand Down Expand Up @@ -174,4 +175,31 @@ void shouldGroupByMessage() {
Pattern actualPattern = capturedGroup.getMessageNamesToKeep().get(0);
assertThat(actualPattern.pattern()).isEqualTo(messages.get(0));
}

@Test
void shouldCustomizeInfoObject() {
// given
SpringwolfConfigProperties.ConfigDocket.Group group = new SpringwolfConfigProperties.ConfigDocket.Group();
group.setGroup("group1");

SpringwolfConfigProperties.ConfigDocket.Info groupInfo = new SpringwolfConfigProperties.ConfigDocket.Info();
groupInfo.setVersion("1.2.3");
groupInfo.setDescription("description-override");

group.setInfo(groupInfo);

when(configDocket.getGroupConfigs()).thenReturn(List.of(group));
Info originalInfo = new Info();
originalInfo.setDescription("description-original");
when(groupedAsyncApi.getInfo()).thenReturn(originalInfo);

// when
Map<String, AsyncAPI> result = asyncApiGroupService.group(asyncAPI);

// then
Info expectedInfo = new Info();
expectedInfo.setVersion("1.2.3");
expectedInfo.setDescription("description-override");
verify(groupedAsyncApi).setInfo(expectedInfo);
}
}

0 comments on commit b882612

Please sign in to comment.