From 3a4ae77c1633f92dbd81a09c31cb95db1e6ea0ba Mon Sep 17 00:00:00 2001 From: elifKurtay Date: Thu, 21 Nov 2024 12:02:24 +0100 Subject: [PATCH] annotation tests --- .../aggregator/AnnotationsAggregator.java | 20 ++++++++++-------- .../generator/SimpleGeneratorSpec.groovy | 21 ++++++++++++++++++- .../src/test/resources/animal.schema.json | 3 ++- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/json-schema-generator/src/main/java/io/micronaut/jsonschema/generator/aggregator/AnnotationsAggregator.java b/json-schema-generator/src/main/java/io/micronaut/jsonschema/generator/aggregator/AnnotationsAggregator.java index 6b4cf94..2bfbcbb 100644 --- a/json-schema-generator/src/main/java/io/micronaut/jsonschema/generator/aggregator/AnnotationsAggregator.java +++ b/json-schema-generator/src/main/java/io/micronaut/jsonschema/generator/aggregator/AnnotationsAggregator.java @@ -98,14 +98,16 @@ public static List getAnnotations(Schema schema, TypeDef property ((int) value) - EXCLUSIVE_DELTA_INT) .build()); } - if (schema.getMaxLength() != null || schema.getMaxItems() != null) { + if (schema.getMaxLength() != null || schema.getMaxItems() != null || schema.getMaxContains() != null) { var value = schema.getMaxLength() != null ? schema.getMaxLength() : schema.getMaxItems(); + value = value == null ? schema.getMaxContains() : value; annotations.add(AnnotationDef .builder(ClassTypeDef.of(SIZE_ANN)) .addMember("max", value).build()); } - if (schema.getMinLength() != null || schema.getMinItems() != null) { + if (schema.getMinLength() != null || schema.getMinItems() != null || schema.getMinContains() != null) { var value = schema.getMinLength() != null ? schema.getMinLength() : schema.getMinItems(); + value = value == null ? schema.getMinContains() : value; annotations.add(AnnotationDef .builder(ClassTypeDef.of(SIZE_ANN)) .addMember("min", value).build()); @@ -126,7 +128,7 @@ public static List getAnnotations(Schema schema, TypeDef property .builder(ClassTypeDef.of(MIN_ANN)) .addMember("value", 1) .build()); - case "^\\d*\\.?\\d+$" -> // positive decimal + case "^\\d*.?\\d+$", "^[1-9][0-9]*.?[0-9]+$" -> // positive decimal annotations.add(AnnotationDef .builder(ClassTypeDef.of(DECIMAL_MIN_ANN)) .addMember("value", "" + EXCLUSIVE_DELTA_DOUBLE) @@ -136,17 +138,17 @@ public static List getAnnotations(Schema schema, TypeDef property .builder(ClassTypeDef.of(MIN_ANN)) .addMember("value", 0) .build()); - case "^-\\d+$", "^-[1-9][0-9]*$" -> // negative int + case "^-d+$", "^-[1-9][0-9]*$" -> // negative int annotations.add(AnnotationDef .builder(ClassTypeDef.of(MAX_ANN)) .addMember("value", 0) .build()); - case "^-\\d*\\.?\\d+$", "^-[1-9][0-9]*\\.?[0-9]+$" -> // negative decimal + case "^-d*.?d+$", "^-[1-9][0-9]*.?[0-9]+$" -> // negative decimal annotations.add(AnnotationDef .builder(ClassTypeDef.of(DECIMAL_MAX_ANN)) .addMember("value", "" + (0.0 - EXCLUSIVE_DELTA_DOUBLE)) .build()); - case "^(-\\d+(\\.\\d+)?|0(\\.0+)?)$", "^-?(0|[1-9][0-9]{0,17})(\\.[0-9]{1,17})?([eE][+-]?[0-9]{1,9}})?$" -> // negative or zero + case "^(-d+(.d+)?|0(.0+)?)$", "^-?(0|[1-9][0-9]{0,17})(.[0-9]{1,17})?([eE][+-]?[0-9]{1,9}})?$" -> // negative or zero annotations.add(AnnotationDef .builder(ClassTypeDef.of(MAX_ANN)) .addMember("value", 0) @@ -160,10 +162,10 @@ public static List getAnnotations(Schema schema, TypeDef property if (schema.getFormat() != null && schema.getFormat().equals("email")) { annotations.add(AnnotationDef.builder(ClassTypeDef.of(EMAIL_ANN)).build()); } - if (schema.getConstValue() != null) { - if (schema.getConstValue().equals("true")) { + if (schema.getConstValue() != null && propertyType.equals(TypeDef.Primitive.BOOLEAN)) { + if (schema.getConstValue().toString().equals("true")) { annotations.add(AnnotationDef.builder(ClassTypeDef.of(ASSERT_TRUE_ANN)).build()); - } else if (schema.getConstValue().equals("false")) { + } else if (schema.getConstValue().toString().equals("false")) { annotations.add(AnnotationDef.builder(ClassTypeDef.of(ASSERT_FALSE_ANN)).build()); } } diff --git a/json-schema-generator/src/test/groovy/io/micronaut/jsonschema/generator/SimpleGeneratorSpec.groovy b/json-schema-generator/src/test/groovy/io/micronaut/jsonschema/generator/SimpleGeneratorSpec.groovy index 2f89786..4191331 100644 --- a/json-schema-generator/src/test/groovy/io/micronaut/jsonschema/generator/SimpleGeneratorSpec.groovy +++ b/json-schema-generator/src/test/groovy/io/micronaut/jsonschema/generator/SimpleGeneratorSpec.groovy @@ -194,6 +194,8 @@ class SimpleGeneratorSpec extends AbstractGeneratorSpec { // https://json-schema.org/understanding-json-schema/reference/numeric 'integer' | '{"type": "integer"}' | 'int integer' 'test' | '{"type": "number"}' | "float test" + 'test' | '{"type": "number", "pattern": "^[0]|[-+]?[1-9][0-9]*$"}' | "Integer test" + 'test' | '{"type": "number", "pattern": "^[0]|[-+]?[1-9][0-9]*.?[0-9]+$"}' | "Float test" // https://json-schema.org/understanding-json-schema/reference/array 'array' | '{"type": "array", "items": {"type": "string"}}' | "List array" 'array' | '{"type": "array", "uniqueItems": true, "items": {"type": "string"}}' | "Set array" @@ -222,12 +224,29 @@ class SimpleGeneratorSpec extends AbstractGeneratorSpec { where: propertyName | propertySchema | expectedJava - // TODO fill in more test cases 'test' | '{"type": "number", "minimum": 10}' | "@DecimalMin(\"10\") float test" + 'test' | '{"type": "number", "maximum": 10}' | "@DecimalMax(\"10\") float test" + 'test' | '{"type": "number", "exclusiveMaximum": 10.0}' | "@DecimalMax(\"9.999\") float test" + 'test' | '{"type": "number", "exclusiveMinimum": 10.0}' | "@DecimalMin(\"10.001\") float test" + 'test' | '{"type": "number", "pattern": "^[1-9][0-9]*$"}' | "@Min(1) Integer test" + 'test' | '{"type": "number", "pattern": "^[1-9][0-9]*.?[0-9]+$"}' | "@DecimalMin(\"0.001\") Float test" + 'test' | '{"type": "number", "pattern": "^[0]|([1-9][0-9]*)$"}' | "@Min(0) Integer test" + 'test' | '{"type": "number", "pattern": "^-d+$"}' | "@Max(0) Integer test" + 'test' | '{"type": "number", "pattern": "^[0]|[-+]?[1-9][0-9]*$"}' | "Integer test" + 'test' | '{"type": "string", "pattern": "[A-Z]+"}' | "@Pattern(regexp = \"[A-Z]+\") String test" + 'test' | '{"type": "boolean", "const": true}' | "@AssertTrue boolean test" + 'test' | '{"type": "boolean", "const": false}' | "@AssertFalse boolean test" + // array annotations 'array' |'{"type": "array", "items": {"type": "number", "minimum": 10.0}}'| "List<@DecimalMin(\"10.0\") Float> array" 'arrayMulti' |'{"type": "array", "items": {"type": "array", ' + '"items": {"type": "number", "minimum": 10.0}, ' + '"uniqueItems": true, "minItems": 2}, "minItems": 1}' | "@Size(min = 1) List<@Size(min = 2) Set<@DecimalMin(\"10.0\") Float>> arrayMulti" + 'array' |'{"type": "array", "contains": {"type": "number"}, ' + + '"minContains": 2, "maxContains": 3}' | "@Size(max = 3) @Size(min = 2) List array" + 'array' |'{"type": "array", "items": {"type": "number"}, ' + + '"minLength": 2, "maxLength": 3}' | "@Size(max = 3) @Size(min = 2) List array" + 'array' |'{"type": "array", "items": {"type":"number"},"nullable": true}' | "@Nullable List array" + 'array' |'{"type": "array", "items": {"type":"number"},"nullable": false}'| "@NotNull List array" } } diff --git a/test-suite-generator/src/test/resources/animal.schema.json b/test-suite-generator/src/test/resources/animal.schema.json index c98e6dd..939b37d 100644 --- a/test-suite-generator/src/test/resources/animal.schema.json +++ b/test-suite-generator/src/test/resources/animal.schema.json @@ -92,7 +92,8 @@ }, "hasMate": { "description": "True when the dog has found their mate.", - "$ref": "#/definitions/boolean" + "$ref": "#/definitions/boolean", + "const": true }, "nickname" : { "description": "A nickname of a good doggo.",