I was using JHipster, with its autogenerated code. And ran into the following issue regarding detecting whether the request is valid or not based on the following: I have a schema of the following:
movie:
type: object
properties:
title:
type: string
director:
type: string
description:
type: string
rating:
type: number
maximum: 5
minimum: 0
example:
title: The Dark Knight
director: Christopher Nolan
description: |
When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman must accept one of the greatest psychological and physical tests of his ability to fight injustice.
rating: 4.5
required:
- title
- director
- description
- rating
And I have a test case that invokes the API with the body:
{
"director":"director",
"title":true,
"description":"description",
"rating":4.5
}
However, I am getting the response
{
"title": "true",
"director": "director",
"description": "description",
"rating": 4.5
}
It is not detecting that the parameter "title" is of type boolean not string, causing the testcase to fail. Interestingly, the error validation detects the error when it is set to null, but not when it is boolean.
I have tried debugging by looking at the way the json is parsed by JHipster. However, it is parsing it as a string, not as a Boolean value.
The implemented delegator
@Service
@Slf4j
@RequiredArgsConstructor
public class Api implements DefaultApiDelegate {
static ObjectMapper mapper = new ObjectMapper();
private final NativeWebRequest nativeWebRequest = null;
static List<Movie> movies = null;
private final ApplicationProperties applicationProperties = new ApplicationProperties();
@Value("${jhipster.clientApp.name}")
private String applicationName;
@Override
public Optional<NativeWebRequest> getRequest() {
return Optional.ofNullable(nativeWebRequest);
}
@Override
public ResponseEntity<List<Movie>> allMovieGet() {
getMovies();
System.out.println("GET request");
ResponseEntity<List<Movie>> res = new ResponseEntity<List<Movie>>(movies.subList(0, 24), HttpStatus.OK);;
return res;
}
@Override
public ResponseEntity<Movie> addMoviePost(Movie movie){
getMovies();
System.out.println(movie.toString());
System.out.println(movie.getTitle().getClass());
movies.add(movie);
ResponseEntity<Movie> res = new ResponseEntity<Movie>(movies.get(movies.size()-1), HttpStatus.CREATED);
return res;
}
public void getMovies(){
try {
movies = mapper.readValue(new File("src/main/resources/movies.json"), new TypeReference<List<Movie>>(){});
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I have not made any other changes to the autogenerated code from JHipster's generate-sources. It is a very simple API that I was creating for a prototype