splintor
splintor

Reputation: 10154

How to write swagger API that accepts XML in request body

We are using Swagger to write our REST API. We have a POST service which should accept an XML file in the request body. This is our request definition:

  /services/invoke:
    post:
      tags:
        - invoke
      summary: A request invocation
      operationId: invokeUsingPOST
      consumes:
        - application/xml
      produces:
        - application/xml
      parameters:
        - name: User-Token
          in: header
          description: The user token
          required: false
          type: string
        - in: body
          name: request
          description: invoke request XML
          required: false
          schema:
            type: string
      responses:
        '200':
          description: OK
          schema:
            type: string
        '400':
          description: Bad Operation
        '401':
          description: Unauthorized
        '404':
          description: Forbidden

However, when we generate a Java client code using swagger-codegen, the generated method looks like:

public String invokeUsingPOST (String userToken, Request request)

And the Request class is generated as:

@ApiModel(description = "")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-25T18:45:31.524+02:00")
public class Request   {
  @Override
  public String toString()  {
    StringBuilder sb = new StringBuilder();
    sb.append("class Request {\n");
    sb.append("}");
    return sb.toString();
  }
}

How do I use it to send my XML? Do I have to derive from it and override the toString() method, or is there a better way to do it?

Upvotes: 0

Views: 6998

Answers (2)

Vlado Lesko
Vlado Lesko

Reputation: 55

Quick workaround for me is to add another else if into serialize method for checking if content type is "application/xml"

} else if (contentType.equals("application/xml")) {
            SerializerUtils s = new SerializerUtils();
            return s.serializeRequestBody(contentType, obj);
}

and SerializerUtils have these 2 methods

public class SerializerUtils {

public Marshaller registerSerializer(Class<?> modelClass) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(modelClass);

    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    return jaxbMarshaller;
}

public RequestBody serializeRequestBody(String contentType, Object obj) {
    StringWriter sw = new StringWriter();
    try {
        registerSerializer(obj.getClass()).marshal(obj, sw);

    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return RequestBody.create(MediaType.parse(contentType), sw.toString());
}

This Works for me and i hope it helps

Upvotes: 0

William Cheng
William Cheng

Reputation: 10817

There's a bug, which was addressed about 2 hours ago. Please pull the latest from Swagger-Codegen and the parameter request in the Java API client should be a string instead of model.

Upvotes: 1

Related Questions