Springdoc-openapi: Why extra Request Methods generated?

Created on 15 May 2020  路  5Comments  路  Source: springdoc/springdoc-openapi

I just wonder why I have OPTIONS, HEAD, PATCH while I don't have any match RequestMapping(method = RequestMethod. OPTIONS, HEAD, PATCH

image

Most helpful comment

@amorenew,

By default, @RequestMapping("/topics"), allows all the HTTP methods which explains the result you got.
You should precise your HTTP methods: for example: @GetMapping("/topics") ...

All 5 comments

I tried this filter but it is not working

    @Override
    public void customise(OpenAPI openApi) {
        openApi.getPaths().entrySet().removeIf(path ->
                shouldRemove(path.getValue()));
        super.removeBrokenReferenceDefinitions(openApi);
    }

    boolean shouldRemove(PathItem pathItem) {
        return    (pathItem.getGet() == null
                && pathItem.getPost() == null
                && pathItem.getPut() == null
                && pathItem.getDelete() == null);
    }

@amorenew,

Can you provide with a sample code (HelloController) or Test that reproduces your behaviour?

@bnasslahsen
Full source Code:
courseswagger_demo.zip

My controller

package com.amorenew.course.topic;

import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TopicController {

    @Autowired
    private TopicService topicService;

    @Operation(summary = "This api to get all topics")
    @RequestMapping("/topics")
    public List<Topic> getTopics() {
       return topicService.getAllTopics();
    }

    @RequestMapping("/topics/{id}")
    public Topic getTopic(@PathVariable String id) {
        return topicService.getTopic(id);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/topics")
    public String addTopic(@RequestBody Topic topic) {
        topicService.addTopic(topic);
        return "{\n\"message\":\"Successfully added Topic " + topic.getName()+"\"\n}";
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/topics/")
    public String updateTopic(@RequestBody Topic topic) {
        topicService.updateTopic(topic);
        return "{\n\"message\":\"Successfully updated Topic " + topic.getName()+"\"\n}";
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/topics/{id}")
    public String deleteTopic(@PathVariable String id) {
        topicService.deleteTopic(id);
        return "{\n\"message\":\"Successfully deleted Topic id: " + id+"\"\n}";
    }
}

Result:
image

@amorenew,

By default, @RequestMapping("/topics"), allows all the HTTP methods which explains the result you got.
You should precise your HTTP methods: for example: @GetMapping("/topics") ...

@bnasslahsen Thank you for your support

Was this page helpful?
0 / 5 - 0 ratings