Home > Software engineering >  Aws lambda json deserilisation provides default values for missing properties
Aws lambda json deserilisation provides default values for missing properties

Time:10-10

hey I have a function in lambda with spring cloud functions that takes a dataobject as a input param say (InputObj). The lambda is triggered via an api gateway. The problem is if I leave out some properties of the InputObj and send a request. I get a default value for those missing properties.

What I would need is something like a 400 bad request error to be thrown unless the user provide all properties of the InputObj.

how can i go about doing it.

CodePudding user response:

I say the simplest way to do that is to:

  1. Not have default values. What's the point of having them if you are saying they are not valid.
  2. Have the getter for the property to throw an exception if such property was not set. This way it will fail during serialization resulting in bad HTTP response

CodePudding user response:

Not sure about AWS Lambda, but you can try javax.validation annotations to achieve desired behavior. It works well in Spring Boot

Step 1

public class InputObj {
    @NotNull(message = "Your validation message here")
    private String notNullStringField
    //...
}

Step 2

public void handleRequest(@Valid InputObj obj) {
    //...
}

If null is passed to notNullStringField, MethodArgumentNotValidException with status code 400 and specified message will be thrown

  • Related