Skip to content

Generic type is not used sometimes [SPR-15364] #19928

Closed as not planned
Closed as not planned

Description

@spring-projects-issues

Vitaly Merenkov opened SPR-15364 and commented

Hello, I'm trying to create a hierarchy of REST controllers for objects which could be parents for another objects and not. I have a lot of sub types for parents and children and do not want to duplicate the same resources in every controller even with delegate pattern.

The sample code is attached. I can't reproduce an issue by some reason in this code as in real environment, but in any case it works not as expected.

The base controller from which every controller should be inherited:

public class BaseRESTController<Request extends BaseRequest> {

    @RequestMapping(method = RequestMethod.POST)
    public String create(@RequestBody List<Request> requests) {
        System.out.println("Create parents " + requests);
        return "OK";
    }
}

The base controller for objects which could have children:

public class BaseRESTControllerWithChildren<PRequest extends BaseRequest, CRequest extends ChildRequest> extends BaseRESTController<PRequest> {
    @RequestMapping(path = "/{id}/children", method = RequestMethod.POST)
    public String createResources(@PathVariable(name = "id") String containerID, @RequestBody List<CRequest> requests) {
        System.out.println("Create children " + requests);
        return "OK";
    }
}

The implementation which works as expected:

@RestController
@RequestMapping(path = "/working")
public class WorkingRESTController extends BaseRESTController<ParentRequest>{
    
}

Request example:

[
  {
    "base": "string",
    "field1": "string"
  }
]

Output from logs:

Create parents [test.complexgenericrest.ParentRequest@1dca0c02]

The implementation which doesn't work as expected:

@RestController
@RequestMapping(path = "/not-working")
public class NOTWorkingRESTController extends BaseRESTControllerWithChildren<ParentRequest, ChildRequest> {
    
}

Request example:

[
  {
    "base": "string",
    "field1": "string"
  }
]

Output from logs:

Create parents [test.complexgenericrest.BaseRequest@16641e47]

In second case I have incorrect request type. In real environment I have this error in logs:

2017-03-20 16:52:05.918  WARN 10944 --- [io-8090-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized field "type" (class mycompany.BaseRequest), not marked as ignorable (6 known properties: "domain-id", "name", "description", "fault-status", "monitoring-profile-id", "external-id"])
 at [Source: java.io.PushbackInputStream@4a4c323e; line: 9, column: 14] (through reference chain: java.util.ArrayList[0]->mycompany.BaseRequest["type"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "type" (class mycompany.BaseRequest), not marked as ignorable (6 known properties: "domain-id", "name", "description", "fault-status", "monitoring-profile-id", "external-id"])
 at [Source: java.io.PushbackInputStream@4a4c323e; line: 9, column: 14] (through reference chain: java.util.ArrayList[0]->mycompany.BaseRequest["type"])

The interesting part is that documentation generated by springfox is correct in any case.

Please suggest what should I change in configuration or fix this issue. I think it is related to #19039.


Attachments:

Issue Links:

Activity

  1. added
    type: bug
    status: waiting-for-triage
    and removed
    type: bug
    on Jan 11, 2019
  2. added
    in: web
    on Nov 8, 2021
  3. self-assigned this
    on Oct 24, 2023
  4. added
    status: declined
    and removed
    status: waiting-for-triage
    on Oct 24, 2023
  5. sdeleuze commented on Oct 24, 2023

    @sdeleuze
    Contributor

    Sorry for the delay, I can't find the reference of the related issue but I think this was fixed at some point, and I have verified with your reproducer that the /not-working endpoint is now working as expected with Spring Framework 6.0.13.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webstatus: declined

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions