-
Notifications
You must be signed in to change notification settings - Fork 115
feat: identify array of single type #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,11 @@ import com.jayway.jsonpath.JsonPath | |
| import org.assertj.core.api.BDDAssertions.then | ||
| import org.assertj.core.api.BDDAssertions.thenThrownBy | ||
| import org.everit.json.schema.ArraySchema | ||
| import org.everit.json.schema.BooleanSchema | ||
| import org.everit.json.schema.CombinedSchema | ||
| import org.everit.json.schema.CombinedSchema.ONE_CRITERION | ||
| import org.everit.json.schema.EnumSchema | ||
| import org.everit.json.schema.NumberSchema | ||
| import org.everit.json.schema.ObjectSchema | ||
| import org.everit.json.schema.Schema | ||
| import org.everit.json.schema.StringSchema | ||
|
|
@@ -138,6 +141,15 @@ class JsonSchemaFromFieldDescriptorsGeneratorTest { | |
| whenSchemaGenerated() | ||
|
|
||
| then(schema).isInstanceOf(ArraySchema::class.java) | ||
| then((schema as ArraySchema).allItemSchema).isInstanceOf(CombinedSchema::class.java) | ||
| val combinedSchema = ((schema as ArraySchema).allItemSchema) as CombinedSchema | ||
| then(combinedSchema.criterion).isEqualTo(ONE_CRITERION) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This also matches current behavior when generating JSON schema for arrays with unknown items type: |
||
| then(combinedSchema.subschemas).extracting("class").containsExactlyInAnyOrder( | ||
| ObjectSchema::class.java, | ||
| BooleanSchema::class.java, | ||
| StringSchema::class.java, | ||
| NumberSchema::class.java | ||
| ) | ||
| thenSchemaIsValid() | ||
| thenSchemaValidatesJson("""[{"id": "some"}]""") | ||
| } | ||
|
|
@@ -149,6 +161,17 @@ class JsonSchemaFromFieldDescriptorsGeneratorTest { | |
| whenSchemaGenerated() | ||
|
|
||
| then(schema).isInstanceOf(ArraySchema::class.java) | ||
| then((schema as ArraySchema).allItemSchema).isInstanceOf(ArraySchema::class.java) | ||
| val arrayOfArraySchema = ((schema as ArraySchema).allItemSchema) as ArraySchema | ||
| then(arrayOfArraySchema.allItemSchema).isInstanceOf(CombinedSchema::class.java) | ||
| val combinedSchema = arrayOfArraySchema.allItemSchema as CombinedSchema | ||
| then(combinedSchema.criterion).isEqualTo(ONE_CRITERION) | ||
| then(combinedSchema.subschemas).extracting("class").containsExactlyInAnyOrder( | ||
| ObjectSchema::class.java, | ||
| BooleanSchema::class.java, | ||
| StringSchema::class.java, | ||
| NumberSchema::class.java | ||
| ) | ||
| thenSchemaIsValid() | ||
| thenSchemaValidatesJson("""[[{"id": "some"}]]""") | ||
| } | ||
|
|
@@ -289,6 +312,51 @@ class JsonSchemaFromFieldDescriptorsGeneratorTest { | |
| thenSchemaIsValid() | ||
| } | ||
|
|
||
| @Test | ||
| fun should_specify_accurate_items_type_in_array_when_descriptor_contains_itemsType_in_additionalParameters() { | ||
| givenFieldDescriptorWithArrayOfSingleType() | ||
|
|
||
| whenSchemaGenerated() | ||
|
|
||
| then(schema).isInstanceOf(ArraySchema::class.java) | ||
| then((schema as ArraySchema).allItemSchema).isInstanceOf(StringSchema::class.java) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Relevant part of JSON schema spec: |
||
| thenSchemaIsValid() | ||
| } | ||
|
|
||
| @Test | ||
| fun should_specify_accurate_items_type_in_array_of_array_when_descriptor_contains_itemsType_in_additionalParameters() { | ||
| givenFieldDescriptorWithTopLevelArrayOfArrayOfSingleType() | ||
|
|
||
| whenSchemaGenerated() | ||
|
|
||
| then(schema).isInstanceOf(ArraySchema::class.java) | ||
| then((schema as ArraySchema).allItemSchema).isInstanceOf(ArraySchema::class.java) | ||
| val arrayOfArraySchema = (schema as ArraySchema).allItemSchema as ArraySchema | ||
| then(arrayOfArraySchema.allItemSchema).isInstanceOf(StringSchema::class.java) | ||
| thenSchemaIsValid() | ||
| } | ||
|
|
||
| @Test | ||
| fun should_create_objectSchema_in_arraySchema_when_items_of_array_are_object() { | ||
| fieldDescriptors = listOf( | ||
| FieldDescriptor("thisIsAnArray", "I'm an array", "ARRAY"), | ||
| FieldDescriptor("thisIsAnArray[].numberItem", "I'm a number", "NUMBER"), | ||
| FieldDescriptor("thisIsAnArray[].objectItem", "I'm an object", "OBJECT") | ||
| ) | ||
|
|
||
| whenSchemaGenerated() | ||
ozscheyge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| then(schema).isInstanceOf(ObjectSchema::class.java) | ||
| then((schema as ObjectSchema).definesProperty("thisIsAnArray")).isTrue | ||
| then((schema as ObjectSchema).propertySchemas["thisIsAnArray"]).isInstanceOf(ArraySchema::class.java) | ||
| val objectInArray = ((schema as ObjectSchema).propertySchemas["thisIsAnArray"] as ArraySchema).allItemSchema as ObjectSchema | ||
| then(objectInArray.definesProperty("numberItem")).isTrue | ||
| then(objectInArray.propertySchemas["numberItem"]).isInstanceOf(NumberSchema::class.java) | ||
| then(objectInArray.definesProperty("objectItem")).isTrue | ||
| then(objectInArray.propertySchemas["objectItem"]).isInstanceOf(ObjectSchema::class.java) | ||
ozscheyge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| thenSchemaIsValid() | ||
| } | ||
|
|
||
| private fun thenSchemaIsValid() { | ||
|
|
||
| val report = JsonSchemaFactory.byDefault() | ||
|
|
@@ -331,10 +399,32 @@ class JsonSchemaFromFieldDescriptorsGeneratorTest { | |
| fieldDescriptors = listOf(FieldDescriptor("[]", "some", "ARRAY")) | ||
| } | ||
|
|
||
| private fun givenFieldDescriptorWithArrayOfSingleType() { | ||
| fieldDescriptors = listOf( | ||
| FieldDescriptor( | ||
| "[]", | ||
| "some", | ||
| "ARRAY", | ||
| attributes = Attributes(itemsType = "string") | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| private fun givenFieldDescriptorWithTopLevelArrayOfArrayOfAny() { | ||
| fieldDescriptors = listOf(FieldDescriptor("[][]", "some", "ARRAY")) | ||
| } | ||
|
|
||
| private fun givenFieldDescriptorWithTopLevelArrayOfArrayOfSingleType() { | ||
| fieldDescriptors = listOf( | ||
| FieldDescriptor( | ||
| "[][]", | ||
| "some", | ||
| "ARRAY", | ||
| attributes = Attributes(itemsType = "string") | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| private fun givenFieldDescriptorUnspecifiedArrayItems() { | ||
| fieldDescriptors = listOf(FieldDescriptor("some[]", "some", "ARRAY")) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this case obsolete now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not obsolete, it looks like fieldDescriptor.jsonSchemaType() can handle this case. Now all the tests are still green. I think the tests cover this part well but maybe I miss something,. If removing this part creates bugs, then some tests might be missing.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, let's keep it removed. If it creates a regression, we get to know what wasn't tested and can write the test. 😅