|
| 1 | +package com.foo.rest.examples.spring.openapi.v3.httporacle.invalidallow.base |
| 2 | + |
| 3 | +import io.swagger.v3.oas.annotations.Hidden |
| 4 | +import org.springframework.boot.SpringApplication |
| 5 | +import org.springframework.boot.autoconfigure.SpringBootApplication |
| 6 | +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration |
| 7 | +import org.springframework.http.ResponseEntity |
| 8 | +import org.springframework.web.bind.annotation.DeleteMapping |
| 9 | +import org.springframework.web.bind.annotation.GetMapping |
| 10 | +import org.springframework.web.bind.annotation.PathVariable |
| 11 | +import org.springframework.web.bind.annotation.PutMapping |
| 12 | +import org.springframework.web.bind.annotation.RequestMapping |
| 13 | +import org.springframework.web.bind.annotation.RestController |
| 14 | + |
| 15 | + |
| 16 | +@SpringBootApplication(exclude = [SecurityAutoConfiguration::class]) |
| 17 | +@RequestMapping(path = ["/api"]) |
| 18 | +@RestController |
| 19 | +open class HttpInvalidAllowApplication { |
| 20 | + |
| 21 | + companion object { |
| 22 | + @JvmStatic |
| 23 | + fun main(args: Array<String>) { |
| 24 | + SpringApplication.run(HttpInvalidAllowApplication::class.java, *args) |
| 25 | + } |
| 26 | + |
| 27 | + private val products = mutableMapOf<Int, String>() |
| 28 | + private val orders = mutableMapOf<Int, String>() |
| 29 | + |
| 30 | + fun reset() { |
| 31 | + products.clear() |
| 32 | + orders.clear() |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // Faulty resource: DELETE is mapped but hidden from the schema, |
| 37 | + // so OPTIONS Allow lists a verb (DELETE) not documented in OpenAPI. |
| 38 | + |
| 39 | + @GetMapping(path = ["/products/{id}"]) |
| 40 | + open fun getProduct(@PathVariable("id") id: Int): ResponseEntity<String> { |
| 41 | + val value = products[id] ?: return ResponseEntity.status(404).build() |
| 42 | + return ResponseEntity.status(200).body(value) |
| 43 | + } |
| 44 | + |
| 45 | + @PutMapping(path = ["/products/{id}"]) |
| 46 | + open fun putProduct(@PathVariable("id") id: Int): ResponseEntity<Any> { |
| 47 | + val isNew = !products.containsKey(id) |
| 48 | + products[id] = "$id" |
| 49 | + return ResponseEntity.status(if (isNew) 201 else 200).build() |
| 50 | + } |
| 51 | + |
| 52 | + @Hidden |
| 53 | + @DeleteMapping(path = ["/products/{id}"]) |
| 54 | + open fun deleteProduct(@PathVariable("id") id: Int): ResponseEntity<Any> { |
| 55 | + products.remove(id) |
| 56 | + return ResponseEntity.status(204).build() |
| 57 | + } |
| 58 | + |
| 59 | + // Clean resource: Allow matches the schema (ignoring HEAD/OPTIONS). |
| 60 | + @GetMapping(path = ["/orders/{id}"]) |
| 61 | + open fun getOrder(@PathVariable("id") id: Int): ResponseEntity<String> { |
| 62 | + val value = orders[id] ?: return ResponseEntity.status(404).build() |
| 63 | + return ResponseEntity.status(200).body(value) |
| 64 | + } |
| 65 | + |
| 66 | + @PutMapping(path = ["/orders/{id}"]) |
| 67 | + open fun putOrder(@PathVariable("id") id: Int): ResponseEntity<Any> { |
| 68 | + val isNew = !orders.containsKey(id) |
| 69 | + orders[id] = "$id" |
| 70 | + return ResponseEntity.status(if (isNew) 201 else 200).build() |
| 71 | + } |
| 72 | +} |
0 commit comments