OpenLiberty / open-liberty

Open Liberty is a highly composable, fast to start, dynamic application server runtime environment
https://openliberty.io
Eclipse Public License 2.0
1.16k stars 599 forks source link

OpenAPI explorer UI example payload for POST request not valid for json #12580

Open nadiramra opened 4 years ago

nadiramra commented 4 years ago

When using openapi-3.1, there is a resource method that is defined as follows:

 @javax.ws.rs.POST
 @javax.ws.rs.Consumes({"application/xml", "application/json"})
 @javax.ws.rs.Produces({"application/xml", "application/json"})

 public Response alltypes2( @javax.ws.rs.core.Context HttpServletRequest req, Fields bufferin)

where the class Fields is defined as:

@javax.xml.bind.annotation.XmlRootElement(name ="fields")
@javax.xml.bind.annotation.XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.FIELD)
@javax.xml.bind.annotation.XmlType(propOrder={"char_", "int4_"})
public class Fields
{
    @javax.xml.bind.annotation.XmlElement(name="char_")
    private String char_ = "";

    @javax.xml.bind.annotation.XmlElement(name="int4_")
    private Integer int4_ = new Integer("0");

    public Fields() { super(); }

    public void setchar_(String value) { this.char_ = value; }
    public String getchar_() { return char_; }

    public void setint4_(Integer value) { this.int4_ = value; }
    public Integer getint4_() { return int4_; }
}

The sample payload the OpenAPI explorer produces for JSON is:

{
  "getchar_": "string",
  "getint4_": 0
}

which is not valid. The expected sample request should have been:

{
  "char_": "string",
  "int4_": 0
}

If you change media type to XML, the correct fields identifiers are used:

<?xml version="1.0" encoding="UTF-8"?>
<fields>
    <char_>string</char_>
    <int4_>0</int4_>
</fields>

It should be noted that apiDiscovery-1.0 handles the situation correctly. Currently running IBM Liberty 20.0.0.5 with JRE 8.

Azquelt commented 3 years ago

This appears to be a mismatch between how mpOpenApi-1.x and jsonb-1.0 extract property names from getters. openapi-3.1 depends on mpOpenApi-1.0, so it has the same problem.

jsonb-1.0 translates getchar_() as a getter for a property char_ whereas mpOpenApi-1.x translates it as a property getchar_.

In mpOpenApi-1.x and openapi-3.1, you can work around this by changing the method name to getChar_()

In mpOpenApi-2.0, this problem is not present so moving up to mpOpenApi-2.0 is also an option.

Do either of these solutions work for you?

nadiramra commented 3 years ago

going to mpOpenApi-2.0 is an option.