Skip to content

Custom sorting/position is not honored for V2 #732

Closed
@chadjaros

Description

@chadjaros

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.

Activity

dilipkrish

dilipkrish commented on May 8, 2015

@dilipkrish
Member

Unfortunately that is because of the data structure that swagger-core uses. There is now way for us to fix that. You might want to try and open an issue in swagger-core

chadjaros

chadjaros commented on May 8, 2015

@chadjaros
Author

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.

class SortedSwagger extends Swagger {
    @Override
    public Map<String, Path> getPaths() {
        return paths;
    }
}

I've opened this issue at swagger-core.

dilipkrish

dilipkrish commented on May 8, 2015

@dilipkrish
Member

Thanks for digging deeper! :) Im closing this issue for now

chadjaros

chadjaros commented on May 8, 2015

@chadjaros
Author

This means you will not address it at this time?

dilipkrish

dilipkrish commented on May 8, 2015

@dilipkrish
Member

There is nothing for us to address here 😕

chadjaros

chadjaros commented on May 8, 2015

@chadjaros
Author

If Docket#apiDescriptionOrdering and Docket#apiListingReferenceOrdering are non-functional, you may want to indicate that by either adding a javadoc comment (if you expect the issue to eventually be corrected) or deprecating them (if this is not a feature you will support).

dilipkrish

dilipkrish commented on May 8, 2015

@dilipkrish
Member

Just to be clear, we have an internal model that totally works as expected and functional. The api descriptions and api operations will be sorted as expected from springfox's standpoint. We're only using the swagger models as DTOs to handle the serialization of our internal service models. Once the DTO's are fixed to preserve the ordering this problem will go away.

We can certainly add a note to describe the problem and the cause to the Readme. Other than waiting for swagger core to fix this, there is nothing to do here other than that I'm afraid.

yiranlihao

yiranlihao commented on Jun 29, 2017

@yiranlihao

bro,why it does not work in version 2.7.0--the newest version?

yiranlihao

yiranlihao commented on Jun 29, 2017

@yiranlihao
@Bean
public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage(basePackage))
            .paths(PathSelectors.any())
            .build().apiListingReferenceOrdering(new Ordering<ApiListingReference>() {
                @Override
                public int compare(ApiListingReference left, ApiListingReference right) {
                    //System.err.println(left.getPath()+"********************"+right.getDescription());//it works
                    //System.out.println(left.getPosition()+" ----------------------- "+right.getPosition());//it works
                    return left.getPosition()>=right.getPosition()?-1:1;
                }
            }).operationOrdering(new Ordering<Operation>() {
                @Override
                public int compare(Operation left, Operation right) {
                    System.err.println(left.getNotes()+"********************"+right.getTags());//it works not
                    System.out.println(left.getPosition()+" ----------------------- "+right.getPosition());//it works not
                    return left.getPosition()>=right.getPosition()?-1:1;
                }
            });
}

anybody can check it for me?

dilipkrish

dilipkrish commented on Jul 4, 2017

@dilipkrish
Member

The swagger-models do not display sorted versions. thats the problem.

abelsromero

abelsromero commented on Jul 20, 2017

@abelsromero

@yiranlihao not working, getPosition() just returns 0.

abelsromero

abelsromero commented on Jul 20, 2017

@abelsromero

The swagger-models do not display sorted versions. thats the problem.

Does this affect apiListingReferenceOrdering. Because I just implemented a custom method inside apiListingReferenceOrdering and it gets invoked, but the results of the comparator seem to be ignored.

15 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @dilipkrish@gorokhmn@ahoehma@grtessman@jackmahoney

        Issue actions

          Custom sorting/position is not honored for V2 · Issue #732 · springfox/springfox