question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Properties in object constructor are required.

See original GitHub issue

JSON object properties which are not required are being set as required when setting those values in that objects constructor.

public class Example {
    public Example() {
        field1 = 4;
    }
    @JsonPropertyDescription("asdsad")
    private Object field1;
    
    @JsonPropertyDescription("asdsad")
    private Object field2;
}

Then in my schema I see:

"required" : [ "field1"],

This is trouble for me since I never specified.

@JsonProperty(required=true)

and even when I say

@JsonProperty(required=false)

it shows up as required anyway.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mbknorcommented, Jan 11, 2018

Thanks @kog for spotting the explanation.

@mbap: Since you are using Jackson and Java with primitives, your code cannot know if field1 (int) was set to “0” in json or if it was not included in json at all. If you need that sort of info, then field1 must be Integer. It makes no point in setting a field ‘not required’, if your code cannot know if you got it or not.

0reactions
mbapcommented, Jan 11, 2018

Awesome thanks for the clear explanation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object.prototype.constructor - JavaScript - MDN Web Docs
The constructor data property of an Object instance returns a reference to the constructor function that created the instance object.
Read more >
JavaScript Constructors: What You Need to Know - Rollbar
The purpose of a constructor is to create a new object and set values for any existing object properties. What Happens When A...
Read more >
required modifier - C# Reference - Microsoft Learn
The required modifier indicates that the field or property it's applied to must be initialized by all constructors or by using an object ......
Read more >
how do you add properties to an object constructor function
You can simple add a private variable to your constructor: function Hotel(name) { var private = 'private'; this.name = name; };.
Read more >
Constructor, operator "new" - The Modern JavaScript Tutorial
The regular {...} syntax allows us to create one object. But often we need to create many similar objects, like multiple users or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found