Skip to content

Commit

Permalink
Add support for swagger tag ordering (deprecated moved to end)
Browse files Browse the repository at this point in the history
Signed-off-by: Avgustin Marinov <[email protected]>
  • Loading branch information
avgustinmm committed Jan 9, 2025
1 parent d2799f4 commit 7ea1091
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.List;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.extensions.Extension;
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand All @@ -24,6 +26,7 @@
import org.eclipse.hawkbit.mgmt.rest.resource.deprecated.json.model.MgmtAssignedTargetRequestBody;
import org.eclipse.hawkbit.mgmt.rest.resource.deprecated.json.model.MgmtDistributionSetTagAssigmentResult;
import org.eclipse.hawkbit.mgmt.rest.resource.deprecated.json.model.MgmtTargetTagAssigmentResult;
import org.eclipse.hawkbit.rest.OpenApiConfiguration;
import org.eclipse.hawkbit.rest.json.model.ExceptionInfo;
import org.springframework.hateoas.MediaTypes;
import org.springframework.http.MediaType;
Expand All @@ -37,7 +40,8 @@
*/
// no request mapping specified here to avoid CVE-2021-22044 in Feign client
@Deprecated(forRemoval = true)
@Tag(name = "Deprecated Management API", description = "Deprecated REST operations.")
@Tag(name = "Deprecated operations", description = "Deprecated REST operations.",
extensions = @Extension(name = OpenApiConfiguration.X_HAWKBIT, properties = @ExtensionProperty(name = "order", value = "2147483647")))
public interface DeprecatedMgmtRestApi {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public GroupedOpenApi mgmtApi() {
.type(SecurityScheme.Type.HTTP)
.in(SecurityScheme.In.HEADER)
.bearerFormat("JWT")
.scheme("bearer"))))
.scheme("bearer")))
.tags(OpenApiConfiguration.sort(openApi.getTags())))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
*/
package org.eclipse.hawkbit.rest;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.tags.Tag;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -24,6 +30,9 @@ public class OpenApiConfiguration {

public static final String HAWKBIT_SERVER_SWAGGER_ENABLED = "hawkbit.server.swagger.enabled";

public static final String X_HAWKBIT = "x-hawkbit";
public static final String ORDER = "order";

private static final String API_TITLE = "hawkBit REST APIs";
private static final String API_VERSION = "v1";
private static final String DESCRIPTION = """
Expand All @@ -34,4 +43,34 @@ public class OpenApiConfiguration {
public OpenAPI openApi() {
return new OpenAPI().info(new Info().title(API_TITLE).version(API_VERSION).description(DESCRIPTION));
}

public static List<Tag> sort(final List<Tag> tags) {
tags.sort(TAG_COMPARATOR);
return tags;
}

private static final Comparator<Tag> TAG_COMPARATOR = new Comparator<>() {

@Override
public int compare(final Tag o1, final Tag o2) {
final int o1Order = order(o1);
final int o2Order = order(o2);
if (o1Order == o2Order) {
return o1.getName().compareTo(o2.getName());
} else {
return Integer.compare(o1Order, o2Order);
}
}

private static int order(final Tag tag) {
return Optional.ofNullable(tag.getExtensions())
.map(extensions -> extensions.get(X_HAWKBIT))
.filter(extension -> Map.class.isAssignableFrom(extension.getClass()))
.map(Map.class::cast)
.map(propertiesMap -> propertiesMap.get(ORDER))
.map(String.class::cast)
.map(Integer::parseInt)
.orElse(0);
}
};
}

0 comments on commit 7ea1091

Please sign in to comment.