0

I'm generating the sources of an OpenAPI contract with the openapi-generator-maven-plugin maven plugin (version 5.4.0).

I'm using the java generator and the webclient library.

I have one endpoint that is responding either application/octet-stream or application/json :

    "responses": {
      "200": {
        "content": {
          "application/octet-stream": {
            "schema": {
              "type": "string",
              "format": "byte"
            }
          },
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/TheResponseObject"
            }
          }
        }
      },

The problem is that the plugin is just considering the first media type (in the order of declaration) to generate the according java return type.

With the example above, I have to deal with byte[].

If I invert the media types (application/json first), I have to deal with the TheResponseObject.

Is there a way to handle both of them and to get the good format according to the response headers ?

2 Answers 2

2

The question is: what result do you expect from openapi-generator-maven-plugin?

It's main idea is to generate API with methods like:

ResponseEntity<TheResponseObject> get(...) 

now, your schema defines that endpoint may return both application/json and byte[], so, from java perspective API would look like:


ResponseEntity<TheResponseObject> get(...);

ResponseEntity<byte[]> get(...);

which is not possible, because both methods has the same signature. In general it is possible to override mustache template (spring template for example) by placing your own version in resources directory and specifying templateDirectory in plugin configuration, but the best what you can get would look like:

ResponseEntity<java.lang.Object> get(...);

do you really need that?

Sign up to request clarification or add additional context in comments.

2 Comments

In fact, the response type depends on the request body : sometimes we ask for json, sometimes we ask for pdf. So I've changed the contract to always handle byte[] and, according to the content type header, I process the body differently. Thank you for your help.
is it just a different representation of the same data? in that case you actually need a converter POJO->PDF.
1

To handle this, I've changed the contract to always handle byte[] in the response.

        "content": {
          "application/plain": {
            "schema": {
              "type": "string",
              "format": "byte"
            }
          },

Then, instead of calling api.theEndpoint(...), I'm calling api.theEndpointWithHttpInfo(...) which sends back a ResponseEntity<byte[]> object.

According to the content type, I can choose how to process the result :

        if (MediaType.APPLICATION_JSON.isCompatibleWith(reponse.getHeaders().getContentType())) {
            // parse json response (reponse.getBody()) 
        } else {
            // it's a pdf 
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.