Skip to content
This repository has been archived by the owner on Feb 1, 2020. It is now read-only.

JSON-API patch extension #4

Open
nerdstep opened this issue Jul 23, 2018 · 2 comments
Open

JSON-API patch extension #4

nerdstep opened this issue Jul 23, 2018 · 2 comments

Comments

@nerdstep
Copy link

Hey @olOwOlo, I am looking to utilize Elide's support for bulk writes via their support for the JSON-API patch extension, but it doesn't appear this project will accept/route content using application/vnd.api+json; ext=jsonpatch headers. Any suggestions?

@olOwOlo
Copy link
Member

olOwOlo commented Jul 24, 2018

There are two hard-coded parameters contentType and accept, maybe we should change this behavior.

ElideResponse response = elide.patch(JSON_API_CONTENT_TYPE, JSON_API_CONTENT_TYPE,
getJsonApiPath(request, elideProperties.getPrefix()), body, authentication);

Currently, you can try adding your own controller as follows:

@RestController
@RequestMapping(value = "${elide.prefix:/api}")
public class JsonApiPatchExtController {

  private static final Logger logger = LoggerFactory.getLogger(JsonApiPatchExtController.class);
  private static final String JSON_API_PATCH_EXT_TYPE = "application/vnd.api+json;ext=jsonpatch";

  private final Elide elide;
  private final ElideProperties elideProperties;

  @Autowired
  public JsonApiPatchExtController(Elide elide, ElideProperties elideProperties) {
    this.elide = elide;
    this.elideProperties = elideProperties;
  }

  @PatchMapping(value = "/**",
      consumes = {JSON_API_PATCH_EXT_TYPE},
      produces = {JSON_API_PATCH_EXT_TYPE})
  public ResponseEntity<String> elidePatch(
      @RequestBody String body,
      @RequestHeader("Content-Type") String contentType,
      @RequestHeader("Accept") String accept,
      HttpServletRequest request,
      Principal authentication) {
    ElideResponse response = elide.patch(contentType, accept,
        getJsonApiPath(request, elideProperties.getPrefix()), body, authentication);
    return ResponseEntity.status(response.getResponseCode()).body(response.getBody());
  }

  private static String getJsonApiPath(HttpServletRequest request, String prefix) {
    String pathname = (String) request
        .getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    logger.debug("[{}][{}] forward to elide.", request.getMethod(), pathname);
    return pathname.replaceFirst(prefix, "");
  }
}

@nerdstep
Copy link
Author

Yeah might be worthwhile to implement, or perhaps enable it via a setting.

I implemented the above controller for now and it works like a charm.

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants