Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rule: entity creation endpoints with putmapping/postmapping without response status 201 #124

Open
dgroup opened this issue Apr 9, 2022 · 0 comments
Assignees
Labels

Comments

@dgroup
Copy link
Owner

dgroup commented Apr 9, 2022

Summary

Key Value
Rule ...
Ruleset io/github/dgroup/arch4u/pmd/api/arch4u-rest-ruleset.xml
Category Design
Framework Spring
Since 0.2.0
Incidents in past at my experience 1

Rule definition

<rule name="MissingHttpResponseStatus"
      since="0.2.0"
      language="java"
      externalInfoUrl="https://github.com/dgroup/arch4u-pmd/discussions/..."
      message="Avoid endpoints for create action without HTTP response code 201 in case of success: https://github.com/dgroup/arch4u-pmd/discussions/..."
      class="io.github.dgroup.arch4u.pmd....">
    <priority>3</priority>
    <properties>
      <property name="annotations" value="org.springframework.web.bind.annotation.PutMapping|org.springframework.web.bind.annotation.PostMapping"/>
      <property name="responseCodes" value="org.springframework.http.HttpStatus.CREATED"/>
      <property name="methods" value="^create.+$"
    </properties>
</rule>

Why?

According to HTTP standard for response codes once entity successfully created the endpoint should return 201 response code.

@PutMapping("/employees/{id}") // violation as no response code
public ResponseEntity<Employee> create(@Valid @RequestBody Employee employee) {
     // ...
}

@ResponseStatus(HttpStatus.CREATED)
@PutMapping("/employees/{id}") // ok, response code is here
public ResponseEntity<Employee> create(@Valid @RequestBody Employee employee) {
     // ...
}

@PutMapping("/employees/{id}") // ok, as method has no 'creation' semantic
public ResponseEntity<Employee> processIt(@Valid @RequestBody Employee employee) {
     // ...
}

Read more

@dgroup dgroup added the new rule label Apr 9, 2022
@dgroup dgroup self-assigned this Apr 9, 2022
@dgroup dgroup added this to the 0.2.0 milestone Apr 9, 2022
@dgroup dgroup changed the title New rule: create with putmapping without response status 201 New rule: entity creation endpoints with putmapping/postmapping without response status 201 Apr 9, 2022
@dgroup dgroup removed this from the 0.2.0 milestone Apr 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant