I need to add #Json non-null in call level but I am not able to do it from swagger code gen.
Hence, could you please help me with this issue?
you can use the #io.swagger.v3.oas.annotations.media.Schema annotation on your method parameters. This annotation allows you to specify the data type, format, and other properties of the parameter, and you can use it to specify that the parameter is required (non-null) using the required property.
Here is how you might use the #Schema annotation to specify that a method parameter is required:
#GET
#Path("/users/{id}")
public User getUser(
#PathParam("id") #Schema(required = true) String userId
) {
...
}
In this example, the #Schema annotation is used on the userId parameter to specify that it is required. This will ensure that the generated code includes the #Json non-null annotation on the parameter, which will enforce the requirement at runtime.
You can also use the #Schema annotation to specify other properties of the parameter, such as its data type, format, and description. For more information, you can refer to the Swagger Code Gen documentation.
Related
Using Jakarta Been Validation https://hibernate.org/validator/ to validate Java code, I annotate a Classes property by a default #NotEmpty validation anotaton
#Validate
public class A {
..
#NotEmpty
private List<String> myList;
..
}
In a Test I set myList to an empty List, validate that List and expect the violation "must not be empty", which is the default violation message for the standard #NotEmpty annotation. I found that by printing it, but also here https://github.com/hibernate/hibernate-validator/blob/main/engine/src/main/resources/org/hibernate/validator/ValidationMessages.properties
Set<ConstraintViolation<A>> violations = validator.validate(A);
assertThat(violations).anyMatch(having Message("must not be empty", A.class));
This works, but I would like to replace the expected Message string with a reference to the default-string like jakarta.validation.constraints.Null.message, avoiding duplication of Strings. This however does not work:
assertThat(violations).anyMatch(having Message(jakarta.validation.constraints.Null.message, A.class));
Is there any validation expert around, that can show me what I am missing?
one has to load it from the classpath, if provided by /ValidationMessages.properties (or language specific ValidationMessages_<x>(_<y>).properties):
Properties ps = new Properties() ;
ps.load( getClass().getResourceAsStream("/ValidationMessages.properties") ) ;
assertThat(violations).anyMatch(having Message(
ps.getProperty( "jakarta.validation.constraints.Null.message"),
A.class))
if provided/overridden by direct class annotation, one has to use reflection on the specific classes field or method and get its annotation.
In my current project the names of the model class fields are German. The fields are all annotated with #JsonProperty for the English translation of the names. E.g. #JsonProperty(value = "operation"). Is there a way in the configuration that the mapping of the fields is done using the JsonProperty annotation?
Example:
public class Auftrag {
#JsonProperty(value = "orderType")
private String auftragsart;
...
}
public class OrderDto {
private String orderType;
}
MapStruct uses the Java Bean convention to detect the properties. This means that it looks in the getters and setters.
Out-of-the-box you cannot use the #JsonProperty. However, you can create your own AccessorNamingStrategy that will provide the properties based on #JsonProperty. The AccessorNamingStrategy gives you access to the Abstract syntax tree, which means you can look for fields in types, check their annotations and check their values.
Keep in mind that MapStruct will only ask to get the property for a method, so you would need to get the property name, then find the field in the type, then look for the #JsonProperty annotation and its value.
You can read more about the AccessorNamingStrategy here in the documentation.
The following will include only fields that have the JsonView(View.MyView.class) annotation:
#JsonView(View.MyView.class)
#RequestMapping(value = "offer", method=RequestMethod.POST)
public ResponseEntity<MyResponse> offer(...) {}
Question: how can I negate it? Means: include any field except the ones having JsonView(View.MyView.class)? Especially without having to add another annotation on any of the remaining fields?
It's not possible. But by adding the following property, any fields not having a #JsonVniew annotation will be serialized:
spring.jackson.mapper.default-view-inclusion=true
As a result, only a #RequestMapping #JsonView will output only non-annotated fields plus the ones matching the view.
So if I want to exclude only some fields inside a specific view, I just have to give the to be excluded fields a different view that is not used in the #RequestMapping. Then they are ignored automatically.
Using java jersey, I have the following #QueryParam's in my method handler:
#Path("/hello")
handleTestRequest(#QueryParam String name, #QueryParam Integer age)
I know if I do:
http://myaddress/hello?name=something
It will go into that method....
I want to make it so that I can call:
http://myaddress/hello?name=something
And it will also go into that same method. Is there any way I can flag an "optional" PathParam? Does it work with #FormParam too? Or am I required to create a separate method with a different method signature?
In JAX-RS parameters are not mandatory, so if you do not supply an age value, it will be NULL, and your method will still be called.
You can also use #DefaultValue to provide a default age value when it's not present.
The #PathParam parameter and the other parameter-based annotations, #MatrixParam, #HeaderParam, #CookieParam, and #FormParam obey the same rules as #QueryParam.
Reference
You should be able to add the #DefaultValue annotation the age parameter, so that if age isn't supplied, the default value will be used.
#Path("/hello")
handleTestRequest(
#QueryParam("name") String name,
#DefaultValue("-1") #QueryParam("age") Integer age)
According to the Javadocs for #DefaultValue, it should work on all *Param annotations.
Defines the default value of request meta-data that is bound using one of the following annotations: PathParam, QueryParam, MatrixParam, CookieParam, FormParam, or HeaderParam. The default value is used if the corresponding meta-data is not present in the request.
You can always wrap return type in optional, for example: #QueryParam("from") Optional<String> from
I'm using Jackson's readValue() method on an object mapper to read from a JSON file and convert it into my java object.
eg.
mapperObject.readValue( node, MyTargetClass.class )
Are there any annotations that I can set on MyTargetClass to enforce required attributes? For example, if I have a JSON object with properties ABC,DEF and GHI, and my Json is the following
{
"ABC" : "somevalue"
"DEF" : "someothervalue"
}
I want it to fail somehow, and only succeed on the readValue if it contained ABC, DEF and GHI.
You can mark a property as required with the #JsonProperty(required = true) annotation, and it will throw a JsonMappingException during deserialization if the property is missing or null.
Edit: I received a downvote for this without comment. I'd love to know why, since it does exactly the right thing.
Jackson does not include validation functionality, and this is by design (i.e. that is considered out-of-scope). But what is usually used is Bean Validation API implementation.
The nice thing about this is decoupling between data format handling, and validation logic.
This is what frameworks like DropWizard use; and it's the direction JAX-RS (like Jersey) are taking things for JAX-RS 2.0.
If you want to make sure a json field is provided, you have to use the #JsonProperty(value = "fieldName", required = true) annotation as a parameter to the constructor. But this is not enough, also the Constructor should have #JsonCreator annotation.
For example, if you have a field named 'endPoint' and you want o make sure it is provided in the JSON file, then the following code will throw an exception if it is not provided.
#JsonCreator
public QuerySettings(#JsonProperty(value = "endPoint", required = true) String endPoint) {
this.endPoint = endPoint;
}
I found this link helpful to understand the Jackson annotations. It also well explains why required=true is not enough and counter-intuitive to its name.
If you are neither satisfied with using #JsonProperty(required = true) as it works only with #JsonCreator nor with the use of bean validation then one more way of tackling it would be to catch this in your setter methods for the relevant variables.
You can simply check if the variable is null before setting it and throw an IllegalArgumentException or NullPointerException (as preferred by few people)
Note: It depends on how your POJO is defined too, so please make sure that it is going the setter method route for this solution to work.