Skip to content

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

Closed as not planned
Closed as not planned
@spring-projects-issues

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:

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently apply

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions