- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.4k
Fix #5318: allow properties-based Constructor auto-detection with 0-params Constructor #5308
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
          
     Merged
      
      
    
      
        
          +156
        
        
          −12
        
        
          
        
      
    
  
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            21 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      9da10e3
              
                Fix #5246
              
              
                cowtowncoder 6cced68
              
                Actual fix
              
              
                cowtowncoder b03d3b7
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder 918c115
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder 0f62396
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder fa42995
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder 5db5ca2
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder b79f233
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder 0359ab4
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder 0f10f54
              
                ...
              
              
                cowtowncoder a85f41a
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder 28b54d2
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder c6d7990
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder 8937101
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder edf7bea
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder 0c35678
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder 37de81d
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder 542b018
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder a4e34a2
              
                Merge branch '3.x' into tatu/3.0/5246-jsoncreator-no-params
              
              
                cowtowncoder 253fa78
              
                Add configurability
              
              
                cowtowncoder 2bde18b
              
                Last tweaks
              
              
                cowtowncoder File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            95 changes: 95 additions & 0 deletions
          
          95 
        
  src/test/java/tools/jackson/databind/deser/creators/NoParamsCreator5318Test.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package tools.jackson.databind.deser.creators; | ||
|  | ||
| import org.junit.jupiter.api.Test; | ||
|  | ||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonIgnore; | ||
|  | ||
| import tools.jackson.databind.*; | ||
| import tools.jackson.databind.cfg.ConstructorDetector; | ||
| import tools.jackson.databind.exc.MismatchedInputException; | ||
| import tools.jackson.databind.testutil.DatabindTestUtil; | ||
|  | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|  | ||
| // For [databind#5318]: allow auto-detection of Properties-based Constructor | ||
| // even with class having Default (no-parameters) Constructor | ||
| public class NoParamsCreator5318Test extends DatabindTestUtil | ||
| { | ||
| static class Pojo5318Working { | ||
| final int productId; | ||
| final String name; | ||
|  | ||
| public Pojo5318Working() { | ||
| this(0, null); | ||
| } | ||
|  | ||
| public Pojo5318Working(int productId, String name) { | ||
| this.productId = productId; | ||
| this.name = name; | ||
| } | ||
| } | ||
|  | ||
| // No auto-detection, due to explicit annotation for 0-params ctor | ||
| static class Pojo5318Annotated { | ||
| @JsonCreator | ||
| public Pojo5318Annotated() { } | ||
|  | ||
| public Pojo5318Annotated(int productId, String name) { | ||
| throw new IllegalStateException("Should not be called"); | ||
| } | ||
| } | ||
|  | ||
| // No auto-detection, due to explicit ignoral of 0-params ctor | ||
| static class Pojo5318Ignore { | ||
| protected Pojo5318Ignore() { } | ||
|  | ||
| @JsonIgnore | ||
| public Pojo5318Ignore(int productId, String name) { | ||
| throw new IllegalStateException("Should not be called"); | ||
| } | ||
| } | ||
|  | ||
| private final ObjectMapper MAPPER = jsonMapperBuilder() | ||
| .enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | ||
| .build(); | ||
|  | ||
| // For [databind#5318]: intended usage | ||
| @Test | ||
| void creatorDetectionWithNoParamsCtor() throws Exception { | ||
| Pojo5318Working pojo = MAPPER.readValue("{\"productId\":1,\"name\":\"foo\"}", Pojo5318Working.class); | ||
| assertEquals(1, pojo.productId); | ||
| assertEquals("foo", pojo.name); | ||
| } | ||
|  | ||
| // For [databind#5318]: avoid detection with explicit annotation on 0-params ctor | ||
| @Test | ||
| void noCreatorDetectionDueToCreatorAnnotation() throws Exception { | ||
| assertNotNull(MAPPER.readValue("{}", Pojo5318Annotated.class)); | ||
| } | ||
|  | ||
| // For [databind#5318]: avoid detection with explicit ignoral of parameterized ctor | ||
| @Test | ||
| void noCreatorDetectionDueToIgnore() throws Exception { | ||
| assertNotNull(MAPPER.readValue("{}", Pojo5318Ignore.class)); | ||
| } | ||
|  | ||
| // For [databind#5318]: avoid detection when configured to do so (not allow | ||
| // implicit with default ctor) | ||
| @Test | ||
| void noCreatorDetectionDueToFeatureDisabled() throws Exception { | ||
| final ObjectMapper mapper = jsonMapperBuilder() | ||
| .enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | ||
| .constructorDetector(ConstructorDetector.DEFAULT | ||
| .withAllowImplicitWithDefaultConstructor(false)) | ||
| .build(); | ||
| try { | ||
| mapper.readValue("{\"productId\": 1}", Pojo5318Working.class); | ||
| fail("Should not pass"); | ||
| } catch (MismatchedInputException e) { | ||
| verifyException(e, "Unrecognized property \"productId\""); | ||
| } | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix a typo (3.0 API change).