springfox: Custom sorting/position is not honored for V2
Using an annotation such as:
@ApiOperation(
value = "Operation",
notes = "Notes",
position = 2
)
and a custom ordering such as:
docket.apiDescriptionOrdering(new Ordering<ApiDescription>() {
@Override
public int compare(ApiDescription left, ApiDescription right) {
int leftPos = left.getOperations().size() == 1 ? left.getOperations().get(0).getPosition() : 0;
int rightPos = right.getOperations().size() == 1 ? right.getOperations().get(0).getPosition() : 0;
int position = Integer.compare(leftPos, rightPos);
if(position == 0) {
position = left.getPath().compareTo(right.getPath());
}
return position;
}
});
The scanner framework seems to read the documentation and sort it properly. When the data is extracted to the Swagger object at Swagger2Controller.java:74, the ordering seems to be lost.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 26 (6 by maintainers)
Commits related to this issue
- Added a note in javadocs as to why sorting of api descriptions and listing references might be working as expected in swagger 2.0 related to #732 — committed to springfox/springfox by dilipkrish 9 years ago
So let me get this right… If your consumer has to follow a hateoas pattern: ie: Call1 must get a list of objects and returns Id to make call 2 Call2 must use id from Call1 to fetch an object that has a list of objects to fetch for Call3 by Id Call3 must use id from Call2 to retrieve last object
But somewhere, someone decided that Alphabetical was a good solution??? Umm… WHY? Now, I have to explain to all users of my swagger doc that they must start at 2, then goto 3 then goto 1 depending on how my endpoint is alphabetically arranged.
Pure and utter NONSENSE! FIX THIS!
I try new version 2.9.2,operationOrdering and apiListingReferenceOrdering not working!It’s a bug!
A LinkedHashMap preserves insertion order when iterated. If you insert things in sorted order, they will stay that way when retrieved via iteration.
It looks like the offending code is here. It’s certainly something that could be fixed by subclassing the Swagger object and getting rid of the extra sort.
I’ve opened this issue at swagger-core.
I also encountered the same problem. And found an extended solution, I hope to help you, and may submit a PR later. Version 2.7.0 is work.
There are five steps here.
The first step is to add position where needed. For example:
The second step is to support the position field, rewriting
springfox.documentation.spring.web.readers.operation.ApiOperationReader
. For example:The third step is to not reset the sort of api list, rewriting
springfox.documentation.swagger2.mappers.ServiceModelToSwagger2MapperImpl
. For example:The fourth step is to override the default two beans. The last step, specify the collation. For example:
Then see api page!
Back to original post. The position field is deprecated in latest release and is not supported. This very important feature is required for a simple way to order endpoints in a controller with default tags.
it make me just can do this for sorting
@Api(value = "xxx", tags = "1", description = "hello1")
@Api(value = "xxx", tags = "2", description = "hello2")
@Api(value = "xxx", tags = "3", description = "hello3")
@jinyahuan Thanks for the hint! However, to be honest, I don’t like this bootstrap UI and I’d prefer using
springfox-swagger-ui
.Personally, I think this field should not be deprecated nor removed from
springfox-swagger-ui
- this is embarrassing…@grtessman if you want an open source project fixed contribute to the code yourself. Don’t be so rude to those who maintain it.
Sounds like some BS, removed important feature with no care to fix.