I am fiddling around with openapi at the moment and trying to create an endpoint that consumes a XML file. When creating the models with openapi however it seems all the XML annotations that I'm used to are missing. This is the openapi.yaml I'm using.
openapi: 3.0.1
info:
version: "1.1"
title: xml test
description: some xml test
servers:
- url: 'http://localhost/:8080'
paths:
'/test':
put:
operationId: testMethodNaming
requestBody:
content:
'application/xml':
schema:
$ref: '#/components/schemas/MyRequest'
responses:
'200':
description: 'OK'
components:
schemas:
MyRequest:
type: object
properties:
name:
type: string
xml:
attribute: true
The MyRequest
schema is the thing in question now. Note that I declare the name property as a XML attribute. The generated class looks looks like this:
/**
* MyRequest
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2019-03-12T15:32:37.070386+01:00[Europe/Berlin]")
public class MyRequest {
@JsonProperty("name")
private String name;
public MyRequest name(String name) {
this.name = name;
return this;
}
/**
* Get name
* @return name
*/
@ApiModelProperty(value = "")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MyRequest myRequest = (MyRequest) o;
return Objects.equals(this.name, myRequest.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class MyRequest {\n");
sb.append(" name: ").append(toIndentedString(name)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
I generated this using the spring-boot generator. I would have expected a @XmlAttribute
annotation to be present above the name field. I would also have expected that there would be a @XmlRootElement
on top of the class.
For some reason I cannot run the generated code right now, but it seems if I would send <MyRequest name="foobar">
to the endpoint it would not be able to parse it with that model.
Did I miss some configuration option or anything so it generates the correct annotations?
Looking at the sourcecode of openapi the needed annotations are there
Something is becoming more and more clear to me: right now, the generators of OpenAPITools , as well as it father SwaggerCodeGen, have json as primary target format. Xml is supported true, but more as an option and frankly quite badly. I have recently discovered 3 bugs:
To make it work, I had to customize myself the various mustache templates in order to have correct xml annotations. The workaround is described in the first issue.
Important: also make sure the withXml
option is activated, so that the mustache Pojo template will produce the required xml annotations.
Good luck.