Skip to content

Commit

Permalink
add quarkus filter to sort HTTP response codes in Openapi spec
Browse files Browse the repository at this point in the history
  • Loading branch information
jcschaff committed Jan 22, 2025
1 parent 2f89b20 commit 5e90efc
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.vcell.restq.openapi;

import io.quarkus.smallrye.openapi.OpenApiFilter;
import org.eclipse.microprofile.openapi.OASFilter;
import org.eclipse.microprofile.openapi.models.Operation;
import org.eclipse.microprofile.openapi.models.responses.APIResponse;
import org.eclipse.microprofile.openapi.models.responses.APIResponses;
import org.jboss.jandex.IndexView;

import java.util.Map;
import java.util.TreeMap;

/**
* Filter to sort HTTP responses for each operation in the OpenAPI document (fixing the random reordering of responses).
*/
@OpenApiFilter(OpenApiFilter.RunStage.BUILD)
public class SortApiResponsesFilter implements OASFilter {

public SortApiResponsesFilter(IndexView _view) {
}

@Override
public Operation filterOperation(Operation operation) {
if (operation.getResponses() != null) {
APIResponses responses = operation.getResponses();
Map<String, APIResponse> sortedResponses = new TreeMap<>(responses.getAPIResponses());

// remove old responses which may not be sorted
String[] responseCodes = responses.getAPIResponses().keySet().toArray(new String[0]);
for (String responseCode : responseCodes) {
responses.removeAPIResponse(responseCode);
}

// add back the responses in sorted order
responses.setAPIResponses(sortedResponses);
}
return operation;
}
}

0 comments on commit 5e90efc

Please sign in to comment.