Ask a Question

Use validation annotations from container classes in defining contract constraints

mschechter
Occasional Visitor

Use validation annotations from container classes in defining contract constraints

According to the latest specifications, validation annotations can be applied as element constraints to the container classes (e.g, Map, List, etc.).

 

Could you please let me know if there is an existing way (even I'd have to write a custom ModelConverter) to have these validations applied to the elements in the contract?

 

Here's an example class:

package example;

import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Digits;

@Schema(description = "Numeric data for contract testing.")
public class NumericData {
  @Schema(description = "Cross-reference of decimal values.")
  private Map<
          String,
          @DecimalMax("7999999.9999") @DecimalMin("0") @Digits(integer = 7, fraction = 4)
          BigDecimal>
      mappedDecimals;

  @ArraySchema(
      arraySchema = @Schema(description = "Collection of decimal values."),
      schema =
          @Schema(description = "Individual value."))
  private List<
          @DecimalMax("7999999.9999") @DecimalMin("0") @Digits(integer = 7, fraction = 4)
          BigDecimal>
      decimalCollection;
}

 

What I'd like to see in my contract is the following:

{
  "openapi" : "3.0.1",
  "info" : {
    "title" : "Test Pricer",
    "version" : "v1"
  },
  "servers" : [ {
    "url" : "/context/application"
  } ],
  "paths" : {
    "/test/echo-numeric" : {
      "post" : {
        "operationId" : "echoNumerics",
        "requestBody" : {
          "content" : {
            "application/json" : {
              "schema" : {
                "$ref" : "#/components/schemas/NumericData"
              }
            }
          }
        },
        "responses" : {
          "default" : {
            "description" : "default response",
            "content" : {
              "application/json" : {
                "schema" : {
                  "$ref" : "#/components/schemas/NumericData"
                }
              }
            }
          }
        }
      }
    }
  },
  "components" : {
    "schemas" : {
      "NumericData" : {
        "type" : "object",
        "properties" : {
          "mappedDecimals" : {
            "type" : "object",
            "additionalProperties" : {
              "type" : "number",
              "maximum" : 7999999.9999,
              "minimum" : 0,
              "description" : "Individual value."
            },
            "description" : "Cross-reference of decimal values."
          },
          "decimalCollection" : {
            "type" : "array",
            "description" : "Collection of decimal values.",you could 
            "items" : {
              "maximum" : 7999999.9999,
              "minimum" : 0,
              "type" : "number",
              "description" : "Individual value."
            }
          }
        },
        "description" : "Numeric data for contract testing."
      }
    }
  }
}

 

I've already created a custom ModelConverter that takes the specification from the @Digits annotation and applies it as a simple pattern to the annotated field. I've tried debugging through, and my converter does not get any of the container element annotations when it invoked.

 

It's also not clear to me how to use the @Schema (or @ArraySchema) annotation to get a value-type description in the Map representation.

 

Any information you could provide would be greatly appreciated. Thanks in advance!

cancel
Showing results for 
Search instead for 
Did you mean: