title: How to check if an api response matches a Java class?
body: <p>In my project I have some DTO classes that I use in my REST communication. With Karate I want to create some external e2e/integration tests where I check if the API responses comply with the contract defined in the DTOs. To keep the whole setup DRY I want to avoid manually writing Karate JSON schemas describing the DTOs. Instead I am looking for a way to translate my Java classes into Karate JSON schemas.</p>
  <h3>What I already have:</h3>
  <p>Let's say this is one of my Java DTOs.</p>
  <pre><code>@Getter
  @Setter
  @AllArgsConstructor
  @SuperBuilder
  @JsonInclude(JsonInclude.Include.NON_NULL)
  @JsonPropertyOrder({&quot;elements&quot;, &quot;property_1&quot;, &quot;property_2&quot;})
  public class MyComplexTypeListDTO extends MyBaseAPIResponseDTO {
      private List&lt;MyComplexType&gt; elements;
  }
  </code></pre>
  <p>I have a transformer using <a href="https://github.com/victools/jsonschema-generator" rel="nofollow noreferrer">jsonschema-generator</a> that gives me the <a href="http://json-schema.org/" rel="nofollow noreferrer">JSON-Schema</a> representation of my class.</p>
  <pre><code>private static JsonNode getSchemaFor(Class clazz) {
          JacksonModule module = new JacksonModule();
          ObjectMapper objectMapper = new ObjectMapper();
          SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(objectMapper, SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON)
                  .with(module);
          SchemaGeneratorConfig config = configBuilder.build();
          SchemaGenerator generator = new SchemaGenerator(config);
  
          return generator.generateSchema(clazz);
      }
  </code></pre>
  <p>However Karate's schema definitions are different and the result fails to match. So I either need a translator from Java classes to Karate's schema directly or a translator/other kind of glue between JSON-Schema and Karate's flavor of JSON schema.</p>
  <p>Is there a way to check if an API response matches a Java class in Karate that I am missing? Is there a convenient transformation library for any of the steps I'm missing or do I have to write my own?</p>