sdaschner / jaxrs-analyzer

Creates REST documentation for JAX-RS projects
Apache License 2.0
319 stars 101 forks source link

Wrong result for JAX-RS Response with Generic Collection of enum #164

Closed rodakr closed 6 years ago

rodakr commented 6 years ago

Hi

Following Case:

// Enum Class public enum Car {

Ferrari , Ford, Fiat

}

......................................

@Path("/service") @RequestScoped public class CarService {

@GET @Path("/cars/{id:[0-9][0-9]*}") @Produces(MediaType.APPLICATION_JSON) public Response getCars(@PathParam("id") long id) { List<Car> mycars = new ArrayList<Car>(){}; return Response.ok(mycars).build(); }

}

............................................. Wrong Result : ..............................................

"responses": { "200": { "description": "OK", "headers": { }, "schema": { "type": "array", "enum": [ "Ferrari", "Fiat", "Ford" ] } } }

................................... Expected Result: .....................................

"responses": { "200": { "description": "OK", "headers": { }, "schema": { "type": "array", "items": { "type", "string", "enum": [ "Ferrari", "Fiat", "Ford" ] } } } }

rodakr commented 6 years ago

Generic List<class> of classes creates correct Result ( case TypeRepresentation.ConcreteTypeRepresentation ) But if Generic List<enum> holds "enum" , result is wrong. If Rest Services returns just "enum" Type and not a generic Collection of "enum" like List<enum>, result is correct. It looks like the case with TypeRepresentation.EnumTypeRepresentation and generic Collection of "enum" is not working.

rodakr commented 6 years ago

Using javax.ws.rs.core.GenericEntity<List<enum>> for JAX-RS Service Response also produce wrong Result, just empty reference... in swagger yaml/json

rodakr commented 6 years ago

jars-analyzer version 0.16, running with -b swagger

sdaschner commented 6 years ago

Ok, that's interesting... Yes, I think your assumption with EnumTypeRepresentation goes into the right direction. I don't know if I can find the time to look into it within the next days, but please feel free to verify it using some tests in the mean time :) Have you seen the https://github.com/sdaschner/jaxrs-analyzer-st project? That helps in verifying local builds.

rodakr commented 6 years ago

changing visit(final TypeRepresentation.EnumTypeRepresentation representation) in SchemaBuilder has fixed the problem. Handling in case if inCollection was not present like in visit(final TypeRepresentation.ConcreteTypeRepresentation representation) :-)

rodakr commented 6 years ago

@Override public void visit(final TypeRepresentation.EnumTypeRepresentation representation) { builder.add("type", "string"); if (!representation.getEnumValues().isEmpty()) { final JsonArrayBuilder array = representation.getEnumValues().stream().sorted().collect(Json::createArrayBuilder, JsonArrayBuilder::add, JsonArrayBuilder::add); // if (inCollection) { builder.add("items", Json.createObjectBuilder().add("type", "string").add("enum", array).build()); } else { builder.add("enum", array); } // } }

rodakr commented 6 years ago

basically last if else fix it from code above

rodakr commented 6 years ago

https://github.com/sdaschner/jaxrs-analyzer/pull/165

Successfully tested with quite complex swagger contract :-)

rodakr commented 6 years ago

Hallo Sebastian

Hier noch pull request https://github.com/sdaschner/jaxrs-analyzer/pull/165 https://github.com/sdaschner/jaxrs-analyzer/pull/165 mit dem erfolgreich getesteten fix :-)

Gruss Radek

Am 08.06.2018 um 08:53 schrieb Sebastian Daschner notifications@github.com:

Ok, that's interesting... Yes, I think your assumption with EnumTypeRepresentation goes into the right direction. I don't know if I can find the time to look into it within the next days, but please feel free to verify it using some tests in the mean time :) Have you seen the https://github.com/sdaschner/jaxrs-analyzer-st https://github.com/sdaschner/jaxrs-analyzer-st project? That helps in verifying local builds.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/sdaschner/jaxrs-analyzer/issues/164#issuecomment-395666663, or mute the thread https://github.com/notifications/unsubscribe-auth/AA-YSr6n2272ak8ah0egNcHQLRH8mlJcks5t6h-GgaJpZM4UfJbh.