@Test public void shouldExtractJsonInstanceElementVariant() throws IOException { DynamicActionMetadata metadata = new DynamicActionMetadata.Builder() .inputShape(StepMetadataHelper.NO_SHAPE) .outputShape(new DataShape.Builder() .kind(DataShapeKinds.JSON_INSTANCE) .specification(getSpecification("person-list-instance.json")) .description("person-list-instance") .collectionType("List") .type(Person.class.getName()) .collectionClassName(List.class.getName()) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_COLLECTION) .addVariant(new DataShape.Builder() .kind(DataShapeKinds.JSON_INSTANCE) .specification(getSpecification("person-instance.json")) .type(Person.class.getName()) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .build()) .addVariant(dummyShape(DataShapeKinds.JAVA)) .build()) .build(); DynamicActionMetadata enrichedMetadata = metadataHandler.handle(metadata); Assert.assertEquals(StepMetadataHelper.NO_SHAPE, enrichedMetadata.inputShape()); Assert.assertNotNull(enrichedMetadata.outputShape()); Assert.assertEquals(DataShapeKinds.JSON_INSTANCE, enrichedMetadata.outputShape().getKind()); Assert.assertEquals(DataShapeMetaData.VARIANT_ELEMENT, enrichedMetadata.outputShape().getMetadata(DataShapeMetaData.VARIANT).orElse("")); Assert.assertEquals(getSpecification("person-instance.json"), enrichedMetadata.outputShape().getSpecification()); Assert.assertEquals(2, enrichedMetadata.outputShape().getVariants().size()); Assert.assertEquals("dummy", enrichedMetadata.outputShape().getVariants().get(0).getMetadata().get(DataShapeMetaData.VARIANT)); Assert.assertEquals(DataShapeMetaData.VARIANT_COLLECTION, enrichedMetadata.outputShape().getVariants().get(1).getMetadata().get(DataShapeMetaData.VARIANT)); Assert.assertEquals("person-list-instance", enrichedMetadata.outputShape().getVariants().get(1).getDescription()); }
@Override public DynamicActionMetadata handle(DynamicActionMetadata metadata) { try { if (metadata.outputShape() != null) { DataShape dataShape = new DataShape.Builder() .createFrom(metadata.outputShape()) .decompress() .build(); if (StepMetadataHelper.isUnifiedJsonSchemaShape(dataShape)) { dataShape = new DataShape.Builder() .createFrom(dataShape) .specification(extractUnifiedJsonBodySpec(dataShape.getSpecification())) .build(); } Optional<DataShape> singleElementShape = dataShape.findVariantByMeta(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT); if (singleElementShape.isPresent()) { if (dataShape.equals(singleElementShape.get())) { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(singleElementShape.get()) .build(); } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder() .createFrom(singleElementShape.get()) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, singleElementShape.get(), DataShapeMetaData.VARIANT_ELEMENT)) .build()) .build(); } } DataShape collectionShape = dataShape.findVariantByMeta(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_COLLECTION).orElse(dataShape); String specification = collectionShape.getSpecification(); if (StringUtils.hasText(specification)) { if (collectionShape.getKind() == DataShapeKinds.JSON_SCHEMA) { JsonSchema schema = JsonSchemaUtils.reader().readValue(specification); if (schema.isArraySchema()) { ArraySchema.Items items = schema.asArraySchema().getItems(); if (items.isSingleItems()) { JsonSchema itemSchema = items.asSingleItems().getSchema(); itemSchema.set$schema(schema.get$schema()); return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .specification(JsonUtils.writer().writeValueAsString(itemSchema)) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, collectionShape, DataShapeMetaData.VARIANT_COLLECTION)) .build()) .build(); } } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .build()) .build(); } } else if (collectionShape.getKind() == DataShapeKinds.JSON_INSTANCE) { if (JsonUtils.isJsonArray(specification)) { List<Object> items = JsonUtils.reader().forType(List.class).readValue(specification); if (!items.isEmpty()) { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .specification(JsonUtils.writer().writeValueAsString(items.get(0))) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, collectionShape, DataShapeMetaData.VARIANT_COLLECTION)) .build()) .build(); } } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .build()) .build(); } } } } } catch (IOException e) { LOG.warn("Unable to read output data shape on dynamic meta data inspection", e); } return DynamicActionMetadata.NOTHING; }
SplitMetadataHandler implements StepMetadataHandler { @Override public DynamicActionMetadata handle(DynamicActionMetadata metadata) { try { if (metadata.outputShape() != null) { DataShape dataShape = new DataShape.Builder() .createFrom(metadata.outputShape()) .decompress() .build(); if (StepMetadataHelper.isUnifiedJsonSchemaShape(dataShape)) { dataShape = new DataShape.Builder() .createFrom(dataShape) .specification(extractUnifiedJsonBodySpec(dataShape.getSpecification())) .build(); } Optional<DataShape> singleElementShape = dataShape.findVariantByMeta(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT); if (singleElementShape.isPresent()) { if (dataShape.equals(singleElementShape.get())) { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(singleElementShape.get()) .build(); } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder() .createFrom(singleElementShape.get()) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, singleElementShape.get(), DataShapeMetaData.VARIANT_ELEMENT)) .build()) .build(); } } DataShape collectionShape = dataShape.findVariantByMeta(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_COLLECTION).orElse(dataShape); String specification = collectionShape.getSpecification(); if (StringUtils.hasText(specification)) { if (collectionShape.getKind() == DataShapeKinds.JSON_SCHEMA) { JsonSchema schema = JsonSchemaUtils.reader().readValue(specification); if (schema.isArraySchema()) { ArraySchema.Items items = schema.asArraySchema().getItems(); if (items.isSingleItems()) { JsonSchema itemSchema = items.asSingleItems().getSchema(); itemSchema.set$schema(schema.get$schema()); return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .specification(JsonUtils.writer().writeValueAsString(itemSchema)) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, collectionShape, DataShapeMetaData.VARIANT_COLLECTION)) .build()) .build(); } } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .build()) .build(); } } else if (collectionShape.getKind() == DataShapeKinds.JSON_INSTANCE) { if (JsonUtils.isJsonArray(specification)) { List<Object> items = JsonUtils.reader().forType(List.class).readValue(specification); if (!items.isEmpty()) { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .specification(JsonUtils.writer().writeValueAsString(items.get(0))) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, collectionShape, DataShapeMetaData.VARIANT_COLLECTION)) .build()) .build(); } } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .build()) .build(); } } } } } catch (IOException e) { LOG.warn("Unable to read output data shape on dynamic meta data inspection", e); } return DynamicActionMetadata.NOTHING; } }
SplitMetadataHandler implements StepMetadataHandler { @Override public DynamicActionMetadata handle(DynamicActionMetadata metadata) { try { if (metadata.outputShape() != null) { DataShape dataShape = new DataShape.Builder() .createFrom(metadata.outputShape()) .decompress() .build(); if (StepMetadataHelper.isUnifiedJsonSchemaShape(dataShape)) { dataShape = new DataShape.Builder() .createFrom(dataShape) .specification(extractUnifiedJsonBodySpec(dataShape.getSpecification())) .build(); } Optional<DataShape> singleElementShape = dataShape.findVariantByMeta(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT); if (singleElementShape.isPresent()) { if (dataShape.equals(singleElementShape.get())) { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(singleElementShape.get()) .build(); } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder() .createFrom(singleElementShape.get()) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, singleElementShape.get(), DataShapeMetaData.VARIANT_ELEMENT)) .build()) .build(); } } DataShape collectionShape = dataShape.findVariantByMeta(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_COLLECTION).orElse(dataShape); String specification = collectionShape.getSpecification(); if (StringUtils.hasText(specification)) { if (collectionShape.getKind() == DataShapeKinds.JSON_SCHEMA) { JsonSchema schema = JsonSchemaUtils.reader().readValue(specification); if (schema.isArraySchema()) { ArraySchema.Items items = schema.asArraySchema().getItems(); if (items.isSingleItems()) { JsonSchema itemSchema = items.asSingleItems().getSchema(); itemSchema.set$schema(schema.get$schema()); return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .specification(JsonUtils.writer().writeValueAsString(itemSchema)) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, collectionShape, DataShapeMetaData.VARIANT_COLLECTION)) .build()) .build(); } } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .build()) .build(); } } else if (collectionShape.getKind() == DataShapeKinds.JSON_INSTANCE) { if (JsonUtils.isJsonArray(specification)) { List<Object> items = JsonUtils.reader().forType(List.class).readValue(specification); if (!items.isEmpty()) { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .specification(JsonUtils.writer().writeValueAsString(items.get(0))) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, collectionShape, DataShapeMetaData.VARIANT_COLLECTION)) .build()) .build(); } } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .build()) .build(); } } } } } catch (IOException e) { LOG.warn("Unable to read output data shape on dynamic meta data inspection", e); } return DynamicActionMetadata.NOTHING; }  }
SplitMetadataHandler implements StepMetadataHandler { @Override public DynamicActionMetadata handle(DynamicActionMetadata metadata) { try { if (metadata.outputShape() != null) { DataShape dataShape = new DataShape.Builder() .createFrom(metadata.outputShape()) .decompress() .build(); if (StepMetadataHelper.isUnifiedJsonSchemaShape(dataShape)) { dataShape = new DataShape.Builder() .createFrom(dataShape) .specification(extractUnifiedJsonBodySpec(dataShape.getSpecification())) .build(); } Optional<DataShape> singleElementShape = dataShape.findVariantByMeta(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT); if (singleElementShape.isPresent()) { if (dataShape.equals(singleElementShape.get())) { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(singleElementShape.get()) .build(); } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder() .createFrom(singleElementShape.get()) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, singleElementShape.get(), DataShapeMetaData.VARIANT_ELEMENT)) .build()) .build(); } } DataShape collectionShape = dataShape.findVariantByMeta(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_COLLECTION).orElse(dataShape); String specification = collectionShape.getSpecification(); if (StringUtils.hasText(specification)) { if (collectionShape.getKind() == DataShapeKinds.JSON_SCHEMA) { JsonSchema schema = JsonSchemaUtils.reader().readValue(specification); if (schema.isArraySchema()) { ArraySchema.Items items = schema.asArraySchema().getItems(); if (items.isSingleItems()) { JsonSchema itemSchema = items.asSingleItems().getSchema(); itemSchema.set$schema(schema.get$schema()); return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .specification(JsonUtils.writer().writeValueAsString(itemSchema)) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, collectionShape, DataShapeMetaData.VARIANT_COLLECTION)) .build()) .build(); } } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .build()) .build(); } } else if (collectionShape.getKind() == DataShapeKinds.JSON_INSTANCE) { if (JsonUtils.isJsonArray(specification)) { List<Object> items = JsonUtils.reader().forType(List.class).readValue(specification); if (!items.isEmpty()) { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .specification(JsonUtils.writer().writeValueAsString(items.get(0))) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, collectionShape, DataShapeMetaData.VARIANT_COLLECTION)) .build()) .build(); } } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .build()) .build(); } } } } } catch (IOException e) { LOG.warn("Unable to read output data shape on dynamic meta data inspection", e); } return DynamicActionMetadata.NOTHING; }  @Override boolean canHandle(StepKind kind); @Override DynamicActionMetadata createMetadata(Step step, List<Step> previousSteps, List<Step> subsequentSteps); @Override DynamicActionMetadata handle(DynamicActionMetadata metadata); }
SplitMetadataHandler implements StepMetadataHandler { @Override public DynamicActionMetadata handle(DynamicActionMetadata metadata) { try { if (metadata.outputShape() != null) { DataShape dataShape = new DataShape.Builder() .createFrom(metadata.outputShape()) .decompress() .build(); if (StepMetadataHelper.isUnifiedJsonSchemaShape(dataShape)) { dataShape = new DataShape.Builder() .createFrom(dataShape) .specification(extractUnifiedJsonBodySpec(dataShape.getSpecification())) .build(); } Optional<DataShape> singleElementShape = dataShape.findVariantByMeta(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT); if (singleElementShape.isPresent()) { if (dataShape.equals(singleElementShape.get())) { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(singleElementShape.get()) .build(); } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder() .createFrom(singleElementShape.get()) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, singleElementShape.get(), DataShapeMetaData.VARIANT_ELEMENT)) .build()) .build(); } } DataShape collectionShape = dataShape.findVariantByMeta(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_COLLECTION).orElse(dataShape); String specification = collectionShape.getSpecification(); if (StringUtils.hasText(specification)) { if (collectionShape.getKind() == DataShapeKinds.JSON_SCHEMA) { JsonSchema schema = JsonSchemaUtils.reader().readValue(specification); if (schema.isArraySchema()) { ArraySchema.Items items = schema.asArraySchema().getItems(); if (items.isSingleItems()) { JsonSchema itemSchema = items.asSingleItems().getSchema(); itemSchema.set$schema(schema.get$schema()); return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .specification(JsonUtils.writer().writeValueAsString(itemSchema)) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, collectionShape, DataShapeMetaData.VARIANT_COLLECTION)) .build()) .build(); } } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .build()) .build(); } } else if (collectionShape.getKind() == DataShapeKinds.JSON_INSTANCE) { if (JsonUtils.isJsonArray(specification)) { List<Object> items = JsonUtils.reader().forType(List.class).readValue(specification); if (!items.isEmpty()) { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .specification(JsonUtils.writer().writeValueAsString(items.get(0))) .addAllVariants(StepMetadataHelper.extractVariants(dataShape, collectionShape, DataShapeMetaData.VARIANT_COLLECTION)) .build()) .build(); } } else { return new DynamicActionMetadata.Builder() .createFrom(metadata) .outputShape(new DataShape.Builder().createFrom(collectionShape) .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT) .build()) .build(); } } } } } catch (IOException e) { LOG.warn("Unable to read output data shape on dynamic meta data inspection", e); } return DynamicActionMetadata.NOTHING; }  @Override boolean canHandle(StepKind kind); @Override DynamicActionMetadata createMetadata(Step step, List<Step> previousSteps, List<Step> subsequentSteps); @Override DynamicActionMetadata handle(DynamicActionMetadata metadata);  }