File tree Expand file tree Collapse file tree 5 files changed +55
-6
lines changed
main/java/com/saasquatch/jsonschemainferrer
test/java/com/saasquatch/jsonschemainferrer Expand file tree Collapse file tree 5 files changed +55
-6
lines changed Original file line number Diff line number Diff line change 44import javax .annotation .Nonnull ;
55import com .fasterxml .jackson .databind .JsonNode ;
66
7+ /**
8+ * Interface for extracting {@code enum} groups from samples.
9+ *
10+ * @author sli
11+ * @see EnumExtractors
12+ * @see EnumExtractorInput
13+ */
714@ FunctionalInterface
815public interface EnumExtractor {
916
17+ /**
18+ * @return The <em>group</em> of enums. Note that each group is expected to be not null and not
19+ * empty. All the elements in each group are expected to come directly from the given
20+ * samples if possible to ensure {@link JsonNode#equals(Object)} works correctly.
21+ */
1022 @ Nonnull
1123 Set <Set <? extends JsonNode >> extractEnums (@ Nonnull EnumExtractorInput input );
1224
Original file line number Diff line number Diff line change 44import javax .annotation .Nonnull ;
55import com .fasterxml .jackson .databind .JsonNode ;
66
7+ /**
8+ * Input for {@link EnumExtractor}
9+ *
10+ * @author sli
11+ */
712public final class EnumExtractorInput {
813
914 private final Collection <? extends JsonNode > samples ;
@@ -15,10 +20,18 @@ public final class EnumExtractorInput {
1520 this .specVersion = specVersion ;
1621 }
1722
23+ /**
24+ * @return The current samples
25+ */
26+ @ Nonnull
1827 public Collection <? extends JsonNode > getSamples () {
1928 return samples ;
2029 }
2130
31+ /**
32+ * @return The current {@link SpecVersion}
33+ */
34+ @ Nonnull
2235 public SpecVersion getSpecVersion () {
2336 return specVersion ;
2437 }
Original file line number Diff line number Diff line change @@ -248,6 +248,19 @@ private Set<ObjectNode> processPrimitives(@Nonnull Collection<ValueNode> valueNo
248248 return anyOfs ;
249249 }
250250
251+ @ Nonnull
252+ private ObjectNode enumExtractionResultToSchema (
253+ @ Nonnull Set <? extends JsonNode > enumExtractionResult ) {
254+ Objects .requireNonNull (enumExtractionResult );
255+ if (enumExtractionResult .isEmpty ()) {
256+ throw new IllegalStateException ("Empty enum group encountered" );
257+ }
258+ final ObjectNode schema = newObject ();
259+ schema .set (Consts .Fields .ENUM , newArray (enumExtractionResult ));
260+ processGenericSchemaFeature (schema , enumExtractionResult , null );
261+ return schema ;
262+ }
263+
251264 /**
252265 * Build {@code anyOf} from sample JSONs. Note that all the arrays and objects will be combined.
253266 *
@@ -277,12 +290,7 @@ private Set<ObjectNode> getAnyOfsFromSamples(
277290 }
278291 final Set <ObjectNode > anyOfs = new HashSet <>();
279292 // Enums
280- for (Set <? extends JsonNode > enumExtractionResult : enumExtractionResults ) {
281- final ObjectNode anyOf = newObject ();
282- anyOf .set (Consts .Fields .ENUM , newArray (enumExtractionResult ));
283- processGenericSchemaFeature (anyOf , enumExtractionResult , null );
284- anyOfs .add (anyOf );
285- }
293+ enumExtractionResults .stream ().map (this ::enumExtractionResultToSchema ).forEach (anyOfs ::add );
286294 // Objects
287295 final ObjectNode resultForObjects = processObjects (objectNodes );
288296 if (resultForObjects != null ) {
Original file line number Diff line number Diff line change @@ -51,6 +51,8 @@ public JsonSchemaInferrerBuilder setSpecVersion(@Nonnull SpecVersion specVersion
5151
5252 /**
5353 * Set the {@link IntegerTypePreference}. The default is {@link IntegerTypePreference#IF_ALL}.
54+ *
55+ * @see IntegerTypePreference
5456 */
5557 public JsonSchemaInferrerBuilder setIntegerTypePreference (
5658 @ Nonnull IntegerTypePreference integerTypePreference ) {
@@ -61,13 +63,22 @@ public JsonSchemaInferrerBuilder setIntegerTypePreference(
6163 /**
6264 * Set the {@link IntegerTypeCriterionL}. The default is
6365 * {@link IntegerTypeCriteria#nonFloatingPoint()}.
66+ *
67+ * @see IntegerTypeCriterion
68+ * @see IntegerTypeCriteria
6469 */
6570 public JsonSchemaInferrerBuilder setIntegerTypeCriterion (
6671 @ Nonnull IntegerTypeCriterion integerTypeCriterion ) {
6772 this .integerTypeCriterion = Objects .requireNonNull (integerTypeCriterion );
6873 return this ;
6974 }
7075
76+ /**
77+ * Set the {@link EnumExtractor}. The default is {@link EnumExtractors#noOp()}.
78+ *
79+ * @see EnumExtractor
80+ * @see EnumExtractors
81+ */
7182 public JsonSchemaInferrerBuilder setEnumExtractor (@ Nonnull EnumExtractor enumExtractor ) {
7283 this .enumExtractor = Objects .requireNonNull (enumExtractor );
7384 return this ;
Original file line number Diff line number Diff line change @@ -547,6 +547,11 @@ public void testEnum() {
547547 assertTrue (stream (anyOf )
548548 .anyMatch (_anyOf -> _anyOf .path ("enum" ).get (0 ).textValue ().equals ("TUESDAY" )));
549549 }
550+ {
551+ final JsonSchemaInferrer inferrer = JsonSchemaInferrer .newBuilder ()
552+ .setEnumExtractor (input -> Collections .singleton (Collections .emptySet ())).build ();
553+ assertThrows (IllegalStateException .class , () -> inferrer .inferForSample (null ));
554+ }
550555 }
551556
552557 @ Test
You can’t perform that action at this time.
0 commit comments