Why Swagger created a systemId field in example?
Asked Answered
A

1

15

I have a REST POST function that has the following header:

@POST
@Consumes(value = { MediaType.APPLICATION_JSON + ";charset=utf-8" })
@Produces(value = { MediaType.APPLICATION_JSON + ";charset=utf-8" })
@ApiOperation(value = "Create a document type", notes = "creates a document type from Json and returns the created type", response = Response.class)
@Session(roles = { Role.ROLE_ADMINISTRATOR })
@PublicApi
public Response create(
        @ApiParam(value = "Created DocumentType", required = true)
        @SwaggerDataType(type = 
           com.infor.daf.icp.internal.rest.models.DocumentType.class) 
        com.infor.daf.icp.internal.rest.models.DocumentType documentType) {

When I look at it in Swagger UI, the Swagger creates an example request body. That body has

systemId (string, optional),

in Model view and

systemId : "string"

in the JSON view. But in the whole project there is not a field named systemId. I had checked the request class and its ancestors one by one, and the whole project by search Java. That symbol sequence systemId does not appear even as a substring of another name.

Where does Swagger gets that name and how can I stop it? For I want it to create a valid example, of course.

Edit: The API function itself takes JSON input without problems and correctly composes an object of the declared class.

Imports :

package com....documentarchive.rest.v1

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

The swagger UI looks so:

enter image description here

Edit 2.
I have removed @SwaggerDataType, or replaced it with @RequestBody, but the strange behaviour remains.

I have set the example to be shown as a concrete string with real data:

@ApiParam(example = DOC_TYPE_EXAMPLE, value = "Created DocumentType", required = true) @RequestBody com.infor.daf.icp.internal.rest.models.DocumentType documentType) {
....
    static final private String DOC_TYPE_EXAMPLE = "{'entityModel':\n" +
        "    {'name':'Anatemplate',\n" +
        "     'desc':'Ana-template',\n" +

And even that didn't help! Swagger still generates some senseless string from some distant file (thanks to @xpa1492 for the reference) somewhere on the internet, instead of simply showing out the prepared string.

More edit:

The pom file: https://drive.google.com/file/d/16fOCq5EFZYVBJRPg0HeiP102eRzEaH6W/view?usp=sharing

Amanda answered 20/4, 2018 at 12:26 Comment(25)
Could these people with downvotes say what they dislike, please?Amanda
Which framework is this - SpringFox?Faires
@Faires No. I have added the full list of imports in the edited question.Amanda
What does your DocumentType class look like?Affair
@Affair It is a class, that extends another one, that extends another one, that extends something abstract.. But not a single one has 'systemId' field. IMHO, swagger takes some class from elsewhere.Amanda
Do you have any kind of class loader enabled by default? What is the output of env in your terminal? It may be some class loader is active and interfering with your bytecodePolson
@TarunLalwani The API function itself takes JSON input correctly. It is swagger who uses strange types.Amanda
When you say that systemId shows up in the Swagger UI, do you mean ti shows up in the response or request area?Fourierism
Also, can we see the imports to all the relevant classes in your code.Fourierism
@JoseMartinez Swagger shows request body as a table. To the left is the real content, that you can set. To the right are the Model/Example Value field. The example is created by Swagger from the class shown in SwaggerDataType parameter annotation. Imports are added, sorry.Amanda
Would it be too troublesome to add a screenshot?Fourierism
Last question, do you just want to fix it or is this question an effort to uncover why?Fourierism
I would just moving your Swagger UI version up and down and see if it is a version specific bug and also build on another machine also just to be sure not a system config issuePolson
Why are you using @SwaggerDataType and what's the fully classified class name?Carnotite
@JoseMartinez For me it would be enough to write annotation so, that swagger will be able to create that Example Value field content. But in javax.ws.rs, please. AFAIK, there are no problems with Spring for that, but we don't use it for that project.Amanda
@IndraBasak 1. because AFAIK, it makes swagger to create the example json string for the type. 2. package com....documentarchive.rest.v1. It is really different from the type of the body.Amanda
docs.oracle.com/javase/7/docs/api/org/w3c/dom/DocumentType.html suspiciously has the same properties - is it possible that it is extended from?Brynhild
@Brynhild It seems so... But why had Swagger used some other class from different library not used in the project? And how can I stop it from doing that?Amanda
How did you create the swagger.json?Caucasia
@Amanda could be some class loading or code generation issue... Could you rename your class DocumentType to something different and test? Is this (github.com/kongchen/swagger-maven-plugin/issues/608) you as well?Shope
@Shope What I am doing is a part of a large EE project, that surely somewhere uses that name, so, changing would not be easy. I am solving this problem simultaneously with debugging insides of the function. Now I have not time to check that for the price of stopping the main work, even if I also have thought about that. The referenced post is of my colleague, he is trying to solve the problem, too, only I love SO more :-).Amanda
@Amanda fair dues! It seems like a rather nasty bug in Swagger. The fact that you use fully qualified class name for DocumentType suggests that you have tried few things. Did you try using different (newer or older) version of Swagger?Shope
Could you add the lib versions used and the generated JSON? The current version of swagger source does not contain the systemId string. It would be great if you could provide a small example which can reproduce the behaviour as the one on github is working fine for me as well.Anthropomorphous
@Gangnus, can you quickly provide a minimal git repo with exact pom version you are using? This seems to be plugin bug and would require debuggingPolson
@TarunLalwani I had added a reference to the pom file. I am terribly sorry, I don't know what to put in the minimal repo, because I don't know what causes the problem. If I knew, I wouldn't ask. Sorry that it was not done quickly - we have holidays on 1/May.Amanda
D
1

Seems to have been answered here: https://github.com/kongchen/swagger-maven-plugin/issues/608

Swagger configuration was not loading the Jackson annotation module, ignoring all annotations used. Therefore ApiReader was reading wrong class (https://docs.oracle.com/javase/8/docs/api/org/w3c/dom/DocumentType.html).

Dusty answered 13/12, 2018 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.