stoicflame / enunciate

Build-time enhancement tool for Java-based Web services projects
http://enunciate.webcohesion.com/
Other
480 stars 200 forks source link

Can't define @QueryParam order #676

Open FrancoisClement opened 7 years ago

FrancoisClement commented 7 years ago

Hi,

I can't find a way to preserve my @QueryParam sort order.

For example :

API

package com.realme.services;

import com.realme.entity.Annonce;
import javax.ws.rs.BeanParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

/**
 *
 * @author francois
 */
@Path("/persons")
public class PersonService {

    @GET
    @Path("/")
    public Annonce getPersonQuery(@BeanParam PersonQuery personQuery) {
        return new Annonce();
    }
}

BeanParam :

package com.realme.services;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.QueryParam;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
@XmlType(propOrder = {"one", "two", "three", "four"})
public class PersonQuery {

    /**
     * Should be in 1st position
     */
    @QueryParam("one")
    private int one;

    /**
     * Should be in 2nd position
     */
    @QueryParam("two")
    @DefaultValue("0")
    private int two;
    /**
     * Should be in 3rd position
     */
    @QueryParam("three")
    private int three;
    /**
     * Should be in 4th position
     */
    @QueryParam("four")
    private int four;

    public int getOne() {
        return one;
    }

    public void setOne(int one) {
        this.one = one;
    }

    public int getTwo() {
        return two;
    }

    public void setTwo(int two) {
        this.two = two;
    }

    public int getThree() {
        return three;
    }

    public void setThree(int three) {
        this.three = three;
    }

    public int getFour() {
        return four;
    }

    public void setFour(int four) {
        this.four = four;
    }
}

I would like my @QueryParam appear in documentation in the same order : one, two, three, four. But whatever I try, they appear on alphabetical order :

enunciate

stoicflame commented 7 years ago

I can understand the request, but it's more complicated that it seems.

For one thing, query parameters (by definition and implementation) are order-independent. In other words, a request for /my/resource?one=a&two=b is semantically equivalent to a request for /my/resource?two=b&one=a.

The other thing that needs to be considered is that query parameters are defined by more than just method parameters. You'd need to take into account the field-based query parameters, setter-based query parameters, and custom enunciate-specific query parameters. And that's further complicated by the field-based, setter-based, and enunciate-specific query parameters that are defined in each superclass.

So it's easy to say "preserve my query parameter order," but a practical solution would need to take into account the other cases. And I still can't think of something better than just lexically sorting them, but I'm open to suggestions if you can provide any.

FrancoisClement commented 7 years ago

Thanks for your Quick reply.

I agree that query parameters are order-independent. But in documentation, it would be convenient to sort parameters in a business logic for better comprehension.

I think you could have an annotation to give order (the same way you have @xmltype(propOrder = {"one", "two", "three", "four"}) for sorting json response.

Regards.

stoicflame commented 6 years ago

This enhancement is currently seeking a sponsor. If anybody is willing to sponsor the work, reach out to me and I'd be happy to pick it up.