I have a REST webapp which uses Swagger for the documentation and it's working great except for the body parameter. All the properties of the body are optional
My Project object is generated with an XSD file and i made sure all the element where minOccurs=1 maxOccurs=1 or unbounded and all my attributes are set to use=required. This doesn't seem to have any effect on it. The next thing I tried was adding
@ApiParam(value = "The project body", required = true) @RequestBody ProjectInfo project
But this had no effect either because the Project object itself is already required. Is there anyway to tell Swagger that the properties of Project are also required? I'm using the SpringFox dependency for Swagger.
I managed to get it required by adding @ApiModel and @ApiModelProperty(value = "The unique name of the project", required = true) to my generated model.
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.11
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2016.02.12 at 09:33:59 AM CET
//
package be.smartask.api.model.post;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import javax.xml.bind.annotation.*;
/**
* <p>Java class for anonymous complex type.
* <p>
* <p>The following schema fragment specifies the expected content contained within this class.
* <p>
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "projectInfo")
@ApiModel
public class ProjectInfo {
@XmlAttribute(name = "name", required = true)
protected String name;
/**
* Gets the value of the name property.
*
* @return possible object is
* {@link String }
*/
@ApiModelProperty(value = "The unique name of the project", required = true)
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value allowed object is
* {@link String }
*/
public void setName(String value) {
this.name = value;
}
}
But now my question changed to Can I add Swagger annotations in the XSD file so it will generate by itself?
The Annotation I need was @ApiModelProperty(value = "", required = true)
To get this generated from my quit a challenge but I managed to do it by doing the next steps.
Add this to you pom.xml
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.1</version>
<configuration>
<forceRegenerate>true</forceRegenerate>
<extension>true</extension>
<args>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>1.0.2</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-annotate-plugin-test-annox-annotations</artifactId>
<version>1.0.0</version>
</plugin>
</plugins>
</configuration>
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
</plugin>
The dependency is required or he won't find your annotation at compile time.
In your XSD file add the following
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1"
xmlns:annox="http://annox.dev.java.net"
jaxb:extensionBindingPrefixes="annox">
Now you should be able to add annotation add class level like this
<xs:element name="projectInfo">
<xs:complexType>
<xsd:annotation>
<xsd:appinfo>
<annox:annotate target="getter">@io.swagger.annotations.ApiModel(value = "project")</annox:annotate>
</xsd:appinfo>
</xsd:annotation>
</xs:complexType>
</xs:element>
and at method level like this
<xs:element name="projectInfo">
<xs:complexType>
<xsd:annotation>
<xsd:appinfo>
<annox:annotate target="getter">@io.swagger.annotations.ApiModel(value = "project")</annox:annotate>
</xsd:appinfo>
</xsd:annotation>
<xs:attribute type="xs:string" name="name" use="required">
<xsd:annotation>
<xsd:appinfo>
<annox:annotate target="getter">@io.swagger.annotations.ApiModelProperty(value="Must be unique", required = true)</annox:annotate>
</xsd:appinfo>
</xsd:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
More info can be found here