Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm using springfox-boot-starter 3.0 to build my API documents.

All things are going in a right way except one case. There seems to be no way to show a JSON example in Swagger UI model if my request parameter is a Map or JSONObject like this:

    @PostMapping("/trigger")
    @ApiOperation(value = "trigger something")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "reqForm",
                    value = "{"123123":"123123"}", example = "{"123123":"123123"}"))
    public StandardResult trigger(@RequestBody Map reqForm) throws Exception {
        Object dagId = reqForm.get("dagId");
        Object conf = reqForm.get("conf");
        return StandardResult.succeed(dagService.trigger(dagId, conf));
    }

I just want to show an example value and a example model of the JSON in Swagger UI and I don't want to write any of extra .java file to define the structure.

Other controllers with plenty of .java files to describe structure can be shown in Swagger UI like this:

pic1

pic2

But in this case, the Map shall be a dynamic parameter which would change frequently. So I hope to show a model of that JSON without too many .java files so that others who are reading my document would have a nice experience and I will not have to change .java file every day.

I know how to show the model and examples in Swagger UI by creating multiple Java beans using @ApiModel and @ApiModelProperty. But that may also lead to a dozens of .java files in order to create only one JSON and it is hard to find and update a property while something in JSON was changed.

For example, I'm going to tell others to send a JSON like this:

"dagInfo": {
        "id": 17,
        "tags": [
            "test",
            "task",
            "dag"
        ],
        "interval": "None",
        "dagName": "testDagGenerate",
        "dagCode": "test_dag_generate",
        "dagDescription": "test"
    }

by using @ApiImplicitParams shown below, I can show the example value but no model in Swagger UI.

    @PostMapping("/trigger")
    @ApiOperation(value = "trigger something")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "reqForm",
                    value = "example json", example = ""dagInfo": {
" +
                    "        "id": 17,
" +
                    "        "tags": [
" +
                    "            "test",
" +
                    "            "task",
" +
                    "            "dag"
" +
                    "        ],
" +
                    "        "interval": "None",
" +
                    "        "dagName": "testDagGenerate",
" +
                    "        "dagCode": "test_dag_generate",
" +
                    "        "dagDescription": "test"
" +
                    "    }"))
    public StandardResult trigger(@RequestBody JSONObject reqForm) throws Exception {
        Long dagId = reqForm.getObject("dagId", Long.class);
        JSONObject conf = reqForm.getJSONObject("conf");
        return StandardResult.succeed(dagService.trigger(dagId, conf));
    }

pic3

pic4

I have no idea how to write this model of example JSON directly to Swagger. Or there is no way to define a example model in Swagger UI without any configuration?

The dependency of Maven is shown below:

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-boot-starter</artifactId>
   <version>3.0.0</version>
</dependency>
question from:https://stackoverflow.com/questions/65936721/how-to-show-customized-json-in-swagger-ui

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
366 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...