From 4c735ca2796d0620ecef30e20471a173cec0809b Mon Sep 17 00:00:00 2001 From: "Xeroman.K" <81915068+xeromank@users.noreply.github.com> Date: Fri, 25 Aug 2023 18:47:41 +0900 Subject: [PATCH] feat: apply field optional (#244) * feat: apply field optional * fix lint --- .../restdocs/apispec/jsonschema/ConstraintResolver.kt | 7 ++++--- .../JsonSchemaFromFieldDescriptorsGeneratorTest.kt | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/restdocs-api-spec-jsonschema/src/main/kotlin/com/epages/restdocs/apispec/jsonschema/ConstraintResolver.kt b/restdocs-api-spec-jsonschema/src/main/kotlin/com/epages/restdocs/apispec/jsonschema/ConstraintResolver.kt index a3a8ab3f..e2f7d1e7 100644 --- a/restdocs-api-spec-jsonschema/src/main/kotlin/com/epages/restdocs/apispec/jsonschema/ConstraintResolver.kt +++ b/restdocs-api-spec-jsonschema/src/main/kotlin/com/epages/restdocs/apispec/jsonschema/ConstraintResolver.kt @@ -82,9 +82,10 @@ internal object ConstraintResolver { .minOrNull() } - internal fun isRequired(fieldDescriptor: FieldDescriptor): Boolean = - findConstraints(fieldDescriptor) - .any { constraint -> REQUIRED_CONSTRAINTS.contains(constraint.name) } + internal fun isRequired(fieldDescriptor: FieldDescriptor): Boolean = findConstraints(fieldDescriptor) + .any { constraint -> + REQUIRED_CONSTRAINTS.contains(constraint.name) + } || !fieldDescriptor.optional private fun findConstraints(fieldDescriptor: FieldDescriptor): List = fieldDescriptor.attributes.validationConstraints diff --git a/restdocs-api-spec-jsonschema/src/test/kotlin/com/epages/restdocs/apispec/jsonschema/JsonSchemaFromFieldDescriptorsGeneratorTest.kt b/restdocs-api-spec-jsonschema/src/test/kotlin/com/epages/restdocs/apispec/jsonschema/JsonSchemaFromFieldDescriptorsGeneratorTest.kt index f0c7b1a1..d7299227 100644 --- a/restdocs-api-spec-jsonschema/src/test/kotlin/com/epages/restdocs/apispec/jsonschema/JsonSchemaFromFieldDescriptorsGeneratorTest.kt +++ b/restdocs-api-spec-jsonschema/src/test/kotlin/com/epages/restdocs/apispec/jsonschema/JsonSchemaFromFieldDescriptorsGeneratorTest.kt @@ -655,7 +655,7 @@ class JsonSchemaFromFieldDescriptorsGeneratorTest { ), FieldDescriptor("lineItems[*].quantity.unit", "some", "STRING"), - FieldDescriptor("shippingAddress", "some", "OBJECT"), + FieldDescriptor("shippingAddress", "some", "OBJECT", true), FieldDescriptor("billingAddress", "some", "OBJECT"), FieldDescriptor( "billingAddress.firstName", "some", "STRING", @@ -732,6 +732,7 @@ class JsonSchemaFromFieldDescriptorsGeneratorTest { "pagePositive", "some", "NUMBER", + true, attributes = Attributes( listOf( Constraint(