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

Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • 3
    Could these people with downvotes say what they dislike, please? – Gangnus Apr 20 '18 at 12:35
  • Which framework is this - SpringFox? – Helen Apr 20 '18 at 17:55
  • @Helen No. I have added the full list of imports in the edited question. – Gangnus Apr 24 '18 at 12:23
  • What does your DocumentType class look like? – Hitobat Apr 24 '18 at 12:42
  • @Hitobat 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. – Gangnus Apr 24 '18 at 12:57
  • 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 bytecode – Tarun Lalwani Apr 24 '18 at 13:04
  • @TarunLalwani The API function itself takes JSON input correctly. It is swagger who uses strange types. – Gangnus Apr 24 '18 at 13:14
  • When you say that systemId shows up in the Swagger UI, do you mean ti shows up in the response or request area? – Jose Martinez Apr 24 '18 at 13:33
  • Also, can we see the imports to all the relevant classes in your code. – Jose Martinez Apr 24 '18 at 13:39
  • @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. – Gangnus Apr 24 '18 at 14:50
  • Would it be too troublesome to add a screenshot? – Jose Martinez Apr 24 '18 at 14:53
  • Last question, do you just want to fix it or is this question an effort to uncover why? – Jose Martinez Apr 24 '18 at 15:31
  • 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 issue – Tarun Lalwani Apr 24 '18 at 20:50
  • Why are you using `@SwaggerDataType` and what's the fully classified class name? – Indra Basak Apr 24 '18 at 21:04
  • @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. – Gangnus Apr 24 '18 at 22:06
  • @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. – Gangnus Apr 25 '18 at 06:52
  • 3
    https://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? – xpa1492 Apr 25 '18 at 07:20
  • @xpa1492 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? – Gangnus Apr 25 '18 at 10:24
  • How did you create the swagger.json? – Christian Apr 25 '18 at 12:13
  • @Gangnus could be some class loading or code generation issue... Could you rename your class `DocumentType` to something different and test? Is this (https://github.com/kongchen/swagger-maven-plugin/issues/608) you as well? – diginoise Apr 25 '18 at 12:18
  • @diginoise 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 :-). – Gangnus Apr 25 '18 at 12:53
  • @Gangnus 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? – diginoise Apr 25 '18 at 14:10
  • 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. – Hash Apr 26 '18 at 09:00
  • @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 debugging – Tarun Lalwani May 01 '18 at 05:12
  • @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. – Gangnus May 02 '18 at 10:18

1 Answers1

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).

Mario Varchmin
  • 3,704
  • 4
  • 18
  • 33