How to write swagger API that accepts XML in request body

2,035 views
Skip to first unread message

Shmulik Flint

unread,
Nov 26, 2015, 7:18:03 AM11/26/15
to Swagger

I posted this question in StackOverflow

http://stackoverflow.com/questions/33937787/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

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?


Ron

unread,
Dec 1, 2015, 2:48:56 PM12/1/15
to Swagger
The first step would be to describe your body as an object and not a string.
While Swagger uses a subset of JSON Schema to describe the objects, it can still be used to describe XMLs as well.
It may not cover all cases, but if your XML is basic, it should be fine. I'd suggest reading XML Object part of the spec for more information.
Reply all
Reply to author
Forward
0 new messages