Reputation: 4161
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
Upvotes: 3
Views: 6055
Reputation: 1
If you use openapi-generator-maven-plugin, try to update the plugin version. Fixed for me in version 7.10.0
Upvotes: 0
Reputation: 21
You have to use the configOption "withXML" at the right place. It must be defined within the "configOptions":
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<executions>
<execution>
<id>openapi-generator-generate</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>some.yaml</inputSpec>
<output>target/generated-sources</output>
<generatorName>java</generatorName>
<generateApis>true</generateApis>
<generateApiTests>false</generateApiTests>
<generateApiDocumentation>false</generateApiDocumentation>
<generateModelTests>false</generateModelTests>
<modelPackage>com.model</modelPackage>
<apiPackage>com.client</apiPackage>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
<withXml>true</withXml>
<library>resttemplate</library>
<interfaceOnly>true</interfaceOnly>
<useBeanValidation>true</useBeanValidation>
<!-- <dateLibrary>java8</dateLibrary>-->
<java8>true</java8>
<unhandledException>true</unhandledException>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Upvotes: 2
Reputation: 3913
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.
Upvotes: 2