Cannot use different request body options in the generated api from OpenAPI Spec?

19 views Asked by At

I've a OpenAPI Spec for email API to use sending emails. The provided spec does only have one endpoint which accepts two different content type which is looking good.

But, when I generate the specification file, generated API does only accept first content type which is application/json. (Accepts EmailInput and nothing else) What might be wrong? The ideal usage should be calling the endpoint either for sending an email without attachment or with attachment. Is it possible with this approach or I need to rely on separate operation?

OpenAPI Spec - v3.0.1

  /v1/sendmail:
    summary: New Email
    description: Sends new mail with or without attachments
    post:
      tags:
        - sendEmail
      operationId: sendEmailPost
      summary: Send email
      description: Send email containing text message using JSON content type. Use multipart for emails with attachments
      requestBody:
        description: Email data - sender, recipient, content, attachments
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/EmailInput'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/EmailMultipart'
      responses:
        '200':
          description: OK - Email has been sent
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EmailInput'
              examples:
                email-inputs:
                  value:
                    $ref: examples/email-input.json

Generated API :

    public ResponseEntity<EmailInput> sendEmailPostWithHttpInfo(EmailInput emailInput) throws RestClientException {
        Object localVarPostBody = emailInput;
        
        // verify the required parameter 'emailInput' is set
        if (emailInput == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'emailInput' when calling sendEmailPost");
        }
        

        final MultiValueMap<String, String> localVarQueryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders localVarHeaderParams = new HttpHeaders();
        final MultiValueMap<String, String> localVarCookieParams = new LinkedMultiValueMap<String, String>();
        final MultiValueMap<String, Object> localVarFormParams = new LinkedMultiValueMap<String, Object>();

        final String[] localVarAccepts = { 
            "application/json"
         };
        final List<MediaType> localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
        final String[] localVarContentTypes = { 
            "application/json", "multipart/form-data"
         };
        final MediaType localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);

        String[] localVarAuthNames = new String[] {  };

        ParameterizedTypeReference<EmailInput> localReturnType = new ParameterizedTypeReference<EmailInput>() {};
        return apiClient.invokeAPI("/v1/sendmail", HttpMethod.POST, Collections.<String, Object>emptyMap(), localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, localVarAuthNames, localReturnType);
    }
}
0

There are 0 answers