Generate Swagger from JAX-RS endpoint with external enum definition
See original GitHub issueI want to generate a swagger from a JAX-RS endpoint with an external enumeration definition however the generated swagger directly includes the enumeration into the definition of the model. It implies that the enumeration documentation is not generated but also that the same enumeration is duplicated on the client side.
I use the swagger-jaxrs
dependency to scan my endpoint and generate the swagger json file. This GitHub repository can be used to reproduce the problem.
The JAX-RS endpoint
@Api("hello")
@Path("/helloSwagger")
public class HelloSwagger {
@ApiOperation(value = "Get all unique customers", notes = "Get all customers matching the given search string.", responseContainer = "Set", response = User.class)
@GET
@Path("/getUniqueUsers")
@Produces(MediaType.APPLICATION_JSON)
public Set<User> getUniqueUsers(
@ApiParam(value = "The search string is used to find customer by their name. Not case sensitive.") @QueryParam("search") String searchString,
@ApiParam(value = "Limits the size of the result set", defaultValue = "50") @QueryParam("limit") int limit
) {
return new HashSet<>(Arrays.asList(new User(), new User()));
}
}
The model with the enumeration
public class User {
private String name = "unknown";
private SynchronizationStatus ldap1 = SynchronizationStatus.UNKNOWN;
private SynchronizationStatus ldap2 = SynchronizationStatus.OFFLINE;
@ApiModelProperty(value = "The user name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ApiModelProperty(value = "The synchronization status with the LDAP1")
public SynchronizationStatus getLdap1() {
return ldap1;
}
public void setLdap1(SynchronizationStatus ldap1) {
this.ldap1 = ldap1;
}
public SynchronizationStatus getLdap2() {
return ldap2;
}
public void setLdap2(SynchronizationStatus ldap2) {
this.ldap2 = ldap2;
}
}
@ApiModel("The synchronization status with LDAP instance.")
public enum SynchronizationStatus {
UNKNOWN,
SYNC,
OFFLINE,
CONFLICT
}
An extract of the swagger generated
{
(...)
},
"definitions" : {
"User" : {
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"description" : "The user name"
},
"ldap1" : {
"type" : "string",
"description" : "The synchronization status with the LDAP1",
"enum" : [ "UNKNOWN", "SYNC", "OFFLINE", "CONFLICT" ]
},
"ldap2" : {
"type" : "string",
"enum" : [ "UNKNOWN", "SYNC", "OFFLINE", "CONFLICT" ]
}
}
}
}
}
Expected result
{
(...)
"definitions" : {
"SynchronizationStatus" : {
"description" : "The synchronization status with LDAP instance.",
"enum" : [ "UNKNOWN", "SYNC", "OFFLINE", "CONFLICT" ],
"type" : "string"
},
"User" : {
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"description" : "The user name"
},
"ldap1" : {
"$ref" : "#/definitions/SynchronizationStatus"
},
"ldap2" : {
"$ref" : "#/definitions/SynchronizationStatus"
}
}
}
}
}
Am I doing something wrong or is it a ‘feature’ of the swagger-jaxrs
library ?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Generate Swagger from JAX-RS endpoint with external enum ...
Show activity on this post. I want to generate a swagger from a JAX-RS endpoint with an external enumeration definition however the generated...
Read more >Document Enum in Swagger - Baeldung
Learn how to document enum in Swagger using the Maven plugin and verify the generated JSON document in the Swagger editor.
Read more >Enforce REST API Standard with Swagger — Engineering Blog
Swagger Codegen parses an OpenAPI/Swagger Definition defined in JSON/YAML and generates code using Mustache templates.
Read more >Apache CXF -- JAXRS Services Description
External WADL documents and JAXRS endpoints. WADL Auto Generation at Runtime. Documenting resource classes and methods in generated WADL. Support for Javadoc.
Read more >38.4. Customizing Enumeration Mapping Red Hat JBoss Fuse ...
name, Specifies the name of the generated Java enum type. This value must be a valid Java identifier. ; map, Specifies if the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Is this still a thing ?
I found this in the source code (ModelResolver.java) of 1.5.21-SNAPSHOT. It seems that it is not intendet to create models for enums.
A solution was proposed on Stack Overflow https://stackoverflow.com/a/42311837/1630604. I use the following class based on this solution. It also includes a fix for
minProperties
andmaxProperties
inside maps. Description using the value when empty and ignore of Jersey classes inside the model.