-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add quarkus filter to sort HTTP response codes in Openapi spec
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
vcell-rest/src/main/java/org/vcell/restq/openapi/SortApiResponsesFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |