|
| 1 | +package com.foo.rest.examples.spring.openapi.v3.httporacle.failmodification.base |
| 2 | + |
| 3 | +import org.springframework.boot.SpringApplication |
| 4 | +import org.springframework.boot.autoconfigure.SpringBootApplication |
| 5 | +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration |
| 6 | +import org.springframework.http.ResponseEntity |
| 7 | +import org.springframework.web.bind.annotation.GetMapping |
| 8 | +import org.springframework.web.bind.annotation.PatchMapping |
| 9 | +import org.springframework.web.bind.annotation.PathVariable |
| 10 | +import org.springframework.web.bind.annotation.PostMapping |
| 11 | +import org.springframework.web.bind.annotation.PutMapping |
| 12 | +import org.springframework.web.bind.annotation.RequestBody |
| 13 | +import org.springframework.web.bind.annotation.RequestMapping |
| 14 | +import org.springframework.web.bind.annotation.RestController |
| 15 | + |
| 16 | +@SpringBootApplication(exclude = [SecurityAutoConfiguration::class]) |
| 17 | +@RequestMapping(path = ["/api/resources"]) |
| 18 | +@RestController |
| 19 | +open class FailModificationApplication { |
| 20 | + |
| 21 | + companion object { |
| 22 | + @JvmStatic |
| 23 | + fun main(args: Array<String>) { |
| 24 | + SpringApplication.run(FailModificationApplication::class.java, *args) |
| 25 | + } |
| 26 | + |
| 27 | + private val data = mutableMapOf<Int, ResourceData>() |
| 28 | + private val dataAlreadyExists = mutableMapOf<Int, ResourceData>() |
| 29 | + |
| 30 | + fun reset(){ |
| 31 | + data.clear() |
| 32 | + dataAlreadyExists.clear() |
| 33 | + dataAlreadyExists[0] = ResourceData("existing", 42) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + data class ResourceData( |
| 38 | + var name: String, |
| 39 | + var value: Int |
| 40 | + ) |
| 41 | + |
| 42 | + data class UpdateRequest( |
| 43 | + val name: String, |
| 44 | + val value: Int |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | + @PostMapping(path = ["/empty"]) |
| 49 | + open fun create(@RequestBody body: ResourceData): ResponseEntity<ResourceData> { |
| 50 | + val id = data.size + 1 |
| 51 | + data[id] = body.copy() |
| 52 | + return ResponseEntity.status(201).body(data[id]) |
| 53 | + } |
| 54 | + |
| 55 | + @GetMapping(path = ["/empty/{id}"]) |
| 56 | + open fun get(@PathVariable("id") id: Int): ResponseEntity<ResourceData> { |
| 57 | + val resource = data[id] |
| 58 | + ?: return ResponseEntity.status(404).build() |
| 59 | + return ResponseEntity.status(200).body(resource) |
| 60 | + } |
| 61 | + |
| 62 | + @PutMapping(path = ["/empty/{id}"]) |
| 63 | + open fun put( |
| 64 | + @PathVariable("id") id: Int, |
| 65 | + @RequestBody body: UpdateRequest |
| 66 | + ): ResponseEntity<Any> { |
| 67 | + |
| 68 | + val resource = data[id] |
| 69 | + ?: return ResponseEntity.status(404).build() |
| 70 | + |
| 71 | + // bug: modifies data even though it will return 4xx |
| 72 | + if(body.name != null) { |
| 73 | + resource.name = body.name |
| 74 | + } |
| 75 | + if(body.value != null) { |
| 76 | + resource.value = body.value |
| 77 | + } |
| 78 | + |
| 79 | + // returns 400 Bad Request, but the data was already modified above |
| 80 | + return ResponseEntity.status(400).body("Invalid request") |
| 81 | + } |
| 82 | + |
| 83 | + @PatchMapping(path = ["/empty/{id}"]) |
| 84 | + open fun patch( |
| 85 | + @PathVariable("id") id: Int, |
| 86 | + @RequestBody body: UpdateRequest |
| 87 | + ): ResponseEntity<Any> { |
| 88 | + |
| 89 | + val resource = data[id] |
| 90 | + ?: return ResponseEntity.status(404).build() |
| 91 | + |
| 92 | + // correct: validation first, reject without modifying |
| 93 | + if(body.name == null && body.value == null) { |
| 94 | + return ResponseEntity.status(400).body("No fields to update") |
| 95 | + } |
| 96 | + |
| 97 | + // correct: does NOT modify data, just returns 4xx |
| 98 | + return ResponseEntity.status(403).body("Forbidden") |
| 99 | + } |
| 100 | + |
| 101 | + // pre-populated resource to test that it is not modified by failed PUT |
| 102 | + |
| 103 | + @PostMapping(path = ["/notempty"]) |
| 104 | + open fun createnotempty(@RequestBody body: ResourceData): ResponseEntity<ResourceData> { |
| 105 | + val id = dataAlreadyExists.size + 1 |
| 106 | + dataAlreadyExists[id] = body.copy() |
| 107 | + return ResponseEntity.status(201).body(dataAlreadyExists[id]) |
| 108 | + } |
| 109 | + |
| 110 | + @GetMapping(path = ["/notempty/{id}"]) |
| 111 | + open fun getnotempty(@PathVariable("id") id: Int): ResponseEntity<ResourceData> { |
| 112 | + val resource = dataAlreadyExists[id] |
| 113 | + ?: return ResponseEntity.status(404).build() |
| 114 | + return ResponseEntity.status(200).body(resource) |
| 115 | + } |
| 116 | + |
| 117 | + @PutMapping(path = ["/notempty/{id}"]) |
| 118 | + open fun putnotempty( |
| 119 | + @PathVariable("id") id: Int, |
| 120 | + @RequestBody body: UpdateRequest |
| 121 | + ): ResponseEntity<Any> { |
| 122 | + |
| 123 | + val resource = dataAlreadyExists[id] |
| 124 | + ?: return ResponseEntity.status(404).build() |
| 125 | + |
| 126 | + resource.name = body.name |
| 127 | + resource.value = body.value |
| 128 | + |
| 129 | + // returns 400 Bad Request, but the data was already modified above |
| 130 | + return ResponseEntity.status(400).body("Invalid request") |
| 131 | + } |
| 132 | + |
| 133 | + @PatchMapping(path = ["/notempty/{id}"]) |
| 134 | + open fun patchnotempty( |
| 135 | + @PathVariable("id") id: Int, |
| 136 | + @RequestBody body: UpdateRequest |
| 137 | + ): ResponseEntity<Any> { |
| 138 | + |
| 139 | + val resource = dataAlreadyExists[id] |
| 140 | + ?: return ResponseEntity.status(404).build() |
| 141 | + |
| 142 | + // correct: validation first, reject without modifying |
| 143 | + return ResponseEntity.status(400).body("No fields to update") |
| 144 | + |
| 145 | + // correct: does NOT modify data, just returns 4xx |
| 146 | + return ResponseEntity.status(403).body("Forbidden") |
| 147 | + } |
| 148 | +} |
0 commit comments