SchemaBuilder { private Schema _getSchema(String schemaName, JsonObject oasJsonObject) { if (StringUtil.isEmpty(schemaName)) { throw TalendRuntimeException.createUnexpectedException( "Unable to determine the Schema for the selected endpoint"); } if (_logger.isDebugEnabled()) { _logger.debug("Create schema for: {}", schemaName); } AtomicInteger index = new AtomicInteger(); List<Schema.Field> schemaFields = new ArrayList<>(); Set<String> previousFieldNames = new HashSet<>(); JsonObject schemaJsonObject = _extractSchemaJsonObject( schemaName, oasJsonObject); if (JsonValue.EMPTY_JSON_OBJECT == schemaJsonObject) { throw new OASException( String.format( "OpenAPI specification does not provide schema " + "definition for %s", schemaName)); } _processSchemaJsonObject( null, schemaJsonObject, index, previousFieldNames, schemaFields, oasJsonObject); return Schema.createRecord("Runtime", null, null, false, schemaFields); }  String extractEndpointSchemaName(
		String endpoint, String operation, JsonObject oasJsonObject); Schema getEntitySchema(String entityName, JsonObject oasJsonObject); Schema inferSchema(
		String endpoint, String operation, JsonObject apiSpecJsonObject);  }
@Test public void testBooleanSchemaFieldsForProducts() { String endpoint = "/v1.0/catalogs/{siteId}/product"; Schema schema = _getSchema(endpoint, OASConstants.OPERATION_POST); _assertValidProductSchema(schema); }
@Test public void testInferSchemaForDeleteOperation() { String endpoint = "/v1.0/products/by-externalReferenceCode/{externalReferenceCode}"; Schema schema = _getSchema(endpoint, OASConstants.OPERATION_DELETE); Assert.assertFalse(AvroUtils.isSchemaEmpty(schema)); }
@Test public void testInferSchemaForGetOperation() { String endpoint = "/v1.0/catalogs/{siteId}/products"; Schema schema = _getSchema(endpoint, OASConstants.OPERATION_GET); Assert.assertFalse(AvroUtils.isSchemaEmpty(schema)); _assertValidProductSchema(schema); }
@Test public void testInferSchemaForInsertOperation() { String endpoint = "/v1.0/catalogs/{siteId}/product"; Schema schema = _getSchema(endpoint, OASConstants.OPERATION_POST); Assert.assertFalse(AvroUtils.isSchemaEmpty(schema)); }
@Test public void testInferSchemaForUpdateOperation() { String endpoint = "/v1.0/products/by-externalReferenceCode/{externalReferenceCode}"; Schema schema = _getSchema(endpoint, OASConstants.OPERATION_PATCH); Assert.assertFalse(AvroUtils.isSchemaEmpty(schema)); }
@Test public void testIntegerSchemaFieldsForProducts() { String endpoint = "/v1.0/catalogs/{siteId}/product"; Schema schema = _getSchema(endpoint, OASConstants.OPERATION_POST); List<Schema.Field> fields = schema.getFields(); Assert.assertThat(fields.size(), equalTo(48)); Schema.Field field = schema.getField("configuration_minStockQuantity"); Schema fieldSchema = AvroUtils.unwrapIfNullable(field.schema()); Assert.assertTrue( "OAS integer in nested object maps to AVRO integer", AvroUtils.isSameType(fieldSchema, AvroUtils._int())); }
@Test public void testLongSchemaFieldsForProducts() { String endpoint = "/v1.0/catalogs/{siteId}/product"; Schema schema = _getSchema(endpoint, OASConstants.OPERATION_POST); List<Schema.Field> fields = schema.getFields(); Assert.assertThat(fields.size(), equalTo(48)); Schema.Field field = schema.getField("id"); Schema fieldSchema = AvroUtils.unwrapIfNullable(field.schema()); Assert.assertTrue( "OAS long maps to AVRO long", AvroUtils.isSameType(fieldSchema, AvroUtils._long())); field = schema.getField("subscriptionConfiguration_numberOfLength"); fieldSchema = AvroUtils.unwrapIfNullable(field.schema()); Assert.assertTrue( "OAS long in nested object maps to AVRO long", AvroUtils.isSameType(fieldSchema, AvroUtils._long())); }
@Test public void testStringSchemaFieldsForProducts() { String endpoint = "/v1.0/catalogs/{siteId}/product"; Schema schema = _getSchema(endpoint, OASConstants.OPERATION_POST); List<Schema.Field> fields = schema.getFields(); Assert.assertThat(fields.size(), equalTo(48)); Schema.Field field = schema.getField("productType"); Schema fieldSchema = AvroUtils.unwrapIfNullable(field.schema()); Assert.assertTrue( "OAS string maps to AVRO string", AvroUtils.isSameType(fieldSchema, AvroUtils._string())); field = schema.getField("taxConfiguration_taxCategory"); fieldSchema = AvroUtils.unwrapIfNullable(field.schema()); Assert.assertTrue( "OAS string in nested object maps to AVRO string", AvroUtils.isSameType(fieldSchema, AvroUtils._string())); field = schema.getField("description"); fieldSchema = AvroUtils.unwrapIfNullable(field.schema()); Assert.assertTrue( "OAS dictionary maps to AVRO string", AvroUtils.isSameType(fieldSchema, AvroUtils._string())); field = schema.getField("expando"); fieldSchema = AvroUtils.unwrapIfNullable(field.schema()); Assert.assertTrue( "OAS free form object maps to AVRO string", AvroUtils.isSameType(fieldSchema, AvroUtils._string())); field = schema.getField("categories"); fieldSchema = AvroUtils.unwrapIfNullable(field.schema()); Assert.assertTrue( "OAS string maps to AVRO string", AvroUtils.isSameType(fieldSchema, AvroUtils._string())); }