mulesoft-labs / raml-for-jax-rs

This project is all about two way transformation of JAX-RS-annotated Java code to RAML API description and back.
Other
296 stars 181 forks source link

JSR-303 generation problem #407

Open GustavSchneider opened 5 years ago

GustavSchneider commented 5 years ago

I am currently working on a project which builds on raml-to-jax-rs-maven-plugin to generate types from RAML and JSON schemas. I've run into some problems which I have not been able to resolve. I've made a small example project to demonstrate my problem, the pom.xml file is pretty much identical to the main project.

I want jsr-303 annotations to be generated and the tool manages to generate most of them. The problem is that I want @Valid to be generated for the input of the interface method which I have not found a way to accomplish. For example in the example project we have one single endpoint "myendpoint" which a client should be able to sent POST request to. The types are specified using JSON schema. The abstract method responsible for handling the POST request is currently generated as:

  @POST
  @Produces("application/json")
  @Consumes("application/json")
  PostMyendpointResponse postMyendpoint(MyPostRequest entity);

Because the server uses Jerseys bean validation I want this method to look something like:

  @POST
  @Produces("application/json")
  @Consumes("application/json")
  PostMyendpointResponse postMyendpoint(@Valid MyPostRequest entity);

All the files generated in the build process can be found here. Validation of the input works if I add @Valid manually. What would be the best way of accomplish this? The most relevant part of the pom.xml file looks as follows:

      <execution>
        <phase>generate-sources</phase>
        <goals>
          <goal>generate</goal>
        </goals>
        <configuration>
              <ramlFile>${project.build.resources[0].directory}/api.raml</ramlFile>
              <resourcePackage>com.mycompany.types</resourcePackage>
          <jaxrsVersion>2.1</jaxrsVersion>
          <jsonMapper>jackson2</jsonMapper>
          <jsonMapperConfiguration>     
        <includeHashcodeAndEquals>true</includeHashcodeAndEquals>
        <isIncludeJsr303Annotations>true</isIncludeJsr303Annotations>
          </jsonMapperConfiguration>
          <generateTypesWith>
        <value>jsr303</value>
        <value>jackson</value>
          </generateTypesWith>
            </configuration>
      </execution>  

On a side note I am also a little confused that I was able to get the tool to generate jsr-303 annotation when setting <isIncludeJsr303Annotations> to true but not <includeJsr303Annotations>.

jpbelang commented 5 years ago

That's weird, because I have an example that generates the @Valid annotations in the method:

raml-to-jaxrs/examples/maven-examples/raml-defined-example

That said, something is wrong. I'll check on my train ride home tonight.

As to the parameters, they are ripped from jsonschematopojo doc. I'll check if you have the right one.

jpbelang commented 5 years ago

We are currently experiencing build problems. You can build by hand by checking out raml-java-tools on branch 1.0.6 and building, and checking out this branch to do the same. I'll merge to 3.0.6 this PM.

GustavSchneider commented 5 years ago

The patch seems to solve my problem. Thank you!