Search in sources :

Example 1 with PegasusSchemaParser

use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.

the class TestSchemaTranslator method testToAvroSchema.

private void testToAvroSchema(String schemaText, Object[] row) throws IOException {
    boolean debug = false;
    if (debug)
        System.out.println(schemaText);
    for (int i = 1; i < row.length; i++) {
        Object[] modeInputs = (Object[]) row[i];
        OptionalDefaultMode[] optionalDefaultModes = (OptionalDefaultMode[]) modeInputs[0];
        Object expected = modeInputs[1];
        for (EmbedSchemaMode embedSchemaMode : EmbedSchemaMode.values()) {
            for (OptionalDefaultMode optionalDefaultMode : optionalDefaultModes) {
                DataSchema schema = TestUtil.dataSchemaFromString(schemaText);
                String preTranslateSchemaText = schema.toString();
                Exception exc = null;
                String avroTextFromSchema = null;
                try {
                    avroTextFromSchema = SchemaTranslator.dataToAvroSchemaJson(schema, new DataToAvroSchemaTranslationOptions(optionalDefaultMode, JsonBuilder.Pretty.SPACES, embedSchemaMode));
                    if (debug) {
                        System.out.println("EmbeddedSchema: " + embedSchemaMode + ", OptionalDefaultMode: " + optionalDefaultMode + ", Avro Schema: " + avroTextFromSchema);
                    }
                } catch (Exception e) {
                    exc = e;
                    if (debug) {
                        e.printStackTrace();
                    }
                }
                if (expected instanceof String) {
                    assertNull(exc);
                    String expectedAvroText = (String) expected;
                    if (embedSchemaMode == EmbedSchemaMode.ROOT_ONLY && hasEmbeddedSchema(schema)) {
                        // when embeddedSchema is enabled
                        // for map, array, enums. and records, we embed the original Pegasus schema
                        DataMap expectedAvroDataMap = TestUtil.dataMapFromString(expectedAvroText);
                        DataMap resultAvroDataMap = TestUtil.dataMapFromString(avroTextFromSchema);
                        Object dataProperty = resultAvroDataMap.remove(SchemaTranslator.DATA_PROPERTY);
                        assertEquals(resultAvroDataMap, expectedAvroDataMap);
                        // look for embedded schema
                        assertNotNull(dataProperty);
                        assertTrue(dataProperty instanceof DataMap);
                        Object schemaProperty = ((DataMap) dataProperty).get(SchemaTranslator.SCHEMA_PROPERTY);
                        assertNotNull(schemaProperty);
                        assertTrue(schemaProperty instanceof DataMap);
                        // make sure embedded schema is same as the original schema
                        PegasusSchemaParser schemaParser = TestUtil.schemaParserFromObjects(Arrays.asList(schemaProperty));
                        DataSchema embeddedSchema = schemaParser.topLevelDataSchemas().get(0);
                        assertEquals(embeddedSchema, schema.getDereferencedDataSchema());
                        // look for optional default mode
                        Object optionalDefaultModeProperty = ((DataMap) dataProperty).get(SchemaTranslator.OPTIONAL_DEFAULT_MODE_PROPERTY);
                        assertNotNull(optionalDefaultMode);
                        assertEquals(optionalDefaultModeProperty, optionalDefaultMode.toString());
                    } else {
                        // for unions and primitives, we never embed the pegasus schema
                        if (embedSchemaMode == EmbedSchemaMode.NONE && hasEmbeddedSchema(schema)) {
                            // make sure no embedded schema when
                            DataMap resultAvroDataMap = TestUtil.dataMapFromString(avroTextFromSchema);
                            assertFalse(resultAvroDataMap.containsKey(SchemaTranslator.DATA_PROPERTY));
                        }
                        assertEquals(avroTextFromSchema, expectedAvroText);
                    }
                    String postTranslateSchemaText = schema.toString();
                    assertEquals(preTranslateSchemaText, postTranslateSchemaText);
                    // make sure Avro accepts it
                    Schema avroSchema = Schema.parse(avroTextFromSchema);
                    if (debug)
                        System.out.println("AvroSchema: " + avroSchema);
                    SchemaParser parser = new SchemaParser();
                    ValidationOptions options = new ValidationOptions();
                    options.setAvroUnionMode(true);
                    parser.setValidationOptions(options);
                    parser.parse(avroTextFromSchema);
                    assertFalse(parser.hasError(), parser.errorMessage());
                    if (optionalDefaultMode == DataToAvroSchemaTranslationOptions.DEFAULT_OPTIONAL_DEFAULT_MODE) {
                        // use other dataToAvroSchemaJson
                        String avroSchema2Json = SchemaTranslator.dataToAvroSchemaJson(TestUtil.dataSchemaFromString(schemaText));
                        String avroSchema2JsonCompact = SchemaTranslator.dataToAvroSchemaJson(TestUtil.dataSchemaFromString(schemaText), new DataToAvroSchemaTranslationOptions());
                        assertEquals(avroSchema2Json, avroSchema2JsonCompact);
                        Schema avroSchema2 = Schema.parse(avroSchema2Json);
                        assertEquals(avroSchema2, avroSchema);
                        // use dataToAvroSchema
                        Schema avroSchema3 = SchemaTranslator.dataToAvroSchema(TestUtil.dataSchemaFromString(schemaText));
                        assertEquals(avroSchema3, avroSchema2);
                    }
                    if (modeInputs.length >= 4) {
                        // check if the translated default value is good by using it.
                        // writer schema and Avro JSON value should not include fields with default values.
                        String writerSchemaText = (String) modeInputs[2];
                        String avroValueJson = (String) modeInputs[3];
                        Schema writerSchema = Schema.parse(writerSchemaText);
                        GenericRecord genericRecord = genericRecordFromString(avroValueJson, writerSchema, avroSchema);
                        if (modeInputs.length >= 5) {
                            String genericRecordJson = (String) modeInputs[4];
                            String genericRecordAsString = genericRecord.toString();
                            DataMap expectedGenericRecord = TestUtil.dataMapFromString(genericRecordJson);
                            DataMap resultGenericRecord = TestUtil.dataMapFromString(genericRecordAsString);
                            assertEquals(resultGenericRecord, expectedGenericRecord);
                        }
                    }
                    if (embedSchemaMode == EmbedSchemaMode.ROOT_ONLY && hasEmbeddedSchema(schema)) {
                        // if embedded schema is enabled, translate Avro back to Pegasus schema.
                        // the output Pegasus schema should be exactly same the input schema
                        // taking into account typeref.
                        AvroToDataSchemaTranslationOptions avroToDataSchemaMode = new AvroToDataSchemaTranslationOptions(AvroToDataSchemaTranslationMode.VERIFY_EMBEDDED_SCHEMA);
                        DataSchema embeddedSchema = SchemaTranslator.avroToDataSchema(avroTextFromSchema, avroToDataSchemaMode);
                        assertEquals(embeddedSchema, schema.getDereferencedDataSchema());
                    }
                } else {
                    Class<?> expectedExceptionClass = (Class<?>) expected;
                    String expectedString = (String) modeInputs[2];
                    assertNotNull(exc);
                    assertNull(avroTextFromSchema);
                    assertTrue(expectedExceptionClass.isInstance(exc));
                    assertTrue(exc.getMessage().contains(expectedString), "\"" + exc.getMessage() + "\" does not contain \"" + expectedString + "\"");
                }
            }
        }
    }
}
Also used : PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) DataSchema(com.linkedin.data.schema.DataSchema) Schema(org.apache.avro.Schema) SchemaParser(com.linkedin.data.schema.SchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap) DataSchema(com.linkedin.data.schema.DataSchema) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 2 with PegasusSchemaParser

use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.

the class DataTemplateUtil method parseSchema.

/**
   * Parse data schema in JSON format to obtain a {@link DataSchema}.
   *
   * @param schemaText provides the data schema in JSON format.
   * @param schemaResolver for resolving referenced schemas
   * @return the {@link DataSchema} parsed from the data schema in JSON format.
   * @throws IllegalArgumentException if the data schema in JSON format is invalid or
   *                                  there is more than one top level schema.
   */
public static DataSchema parseSchema(String schemaText, DataSchemaResolver schemaResolver) throws IllegalArgumentException {
    PegasusSchemaParser parser = SchemaParserFactory.instance().create(schemaResolver);
    parser.parse(schemaText);
    if (parser.hasError()) {
        if (debug) {
            out.println(parser.errorMessage());
        }
        throw new IllegalArgumentException(parser.errorMessage());
    }
    if (parser.topLevelDataSchemas().size() != 1) {
        throw new IllegalArgumentException("More than one top level schemas");
    }
    return parser.topLevelDataSchemas().get(0);
}
Also used : PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser)

Example 3 with PegasusSchemaParser

use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.

the class TestSchemaTranslator method testToAvroSchemaInternal.

private void testToAvroSchemaInternal(String schemaText, OptionalDefaultMode[] optionalDefaultModes, String expected, String writerSchemaText, String avroValueJson, String expectedGenericRecordJson) throws IOException {
    for (EmbedSchemaMode embedSchemaMode : EmbedSchemaMode.values()) {
        for (OptionalDefaultMode optionalDefaultMode : optionalDefaultModes) {
            DataSchema schema = TestUtil.dataSchemaFromString(schemaText);
            String preTranslateSchemaText = schema.toString();
            String avroTextFromSchema = null;
            DataToAvroSchemaTranslationOptions transOptions = new DataToAvroSchemaTranslationOptions(optionalDefaultMode, JsonBuilder.Pretty.SPACES, embedSchemaMode);
            transOptions.setTyperefPropertiesExcludeSet(new HashSet<>(Arrays.asList("validate", "java")));
            avroTextFromSchema = SchemaTranslator.dataToAvroSchemaJson(schema, transOptions);
            if (embedSchemaMode == EmbedSchemaMode.ROOT_ONLY && hasEmbeddedSchema(schema)) {
                // when embeddedSchema is enabled
                // for map, array, enums. and records, we embed the original Pegasus schema
                DataMap expectedAvroDataMap = TestUtil.dataMapFromString(expected);
                DataMap resultAvroDataMap = TestUtil.dataMapFromString(avroTextFromSchema);
                Object dataProperty = resultAvroDataMap.remove(SchemaTranslator.DATA_PROPERTY);
                assertEquals(resultAvroDataMap, expectedAvroDataMap);
                // look for embedded schema
                assertNotNull(dataProperty);
                assertTrue(dataProperty instanceof DataMap);
                Object schemaProperty = ((DataMap) dataProperty).get(SchemaTranslator.SCHEMA_PROPERTY);
                assertNotNull(schemaProperty);
                assertTrue(schemaProperty instanceof DataMap);
                // make sure embedded schema is same as the original schema
                PegasusSchemaParser schemaParser = TestUtil.schemaParserFromObjects(Arrays.asList(schemaProperty));
                DataSchema embeddedSchema = schemaParser.topLevelDataSchemas().get(0);
                assertEquals(embeddedSchema, schema.getDereferencedDataSchema());
                // look for optional default mode
                Object optionalDefaultModeProperty = ((DataMap) dataProperty).get(SchemaTranslator.OPTIONAL_DEFAULT_MODE_PROPERTY);
                assertNotNull(optionalDefaultMode);
                assertEquals(optionalDefaultModeProperty, optionalDefaultMode.toString());
            } else {
                // for unions and primitives, we never embed the pegasus schema
                if (embedSchemaMode == EmbedSchemaMode.NONE && hasEmbeddedSchema(schema)) {
                    // make sure no embedded schema when
                    DataMap resultAvroDataMap = TestUtil.dataMapFromString(avroTextFromSchema);
                    assertFalse(resultAvroDataMap.containsKey(SchemaTranslator.DATA_PROPERTY));
                }
                assertEquals(avroTextFromSchema, expected);
            }
            String postTranslateSchemaText = schema.toString();
            assertEquals(postTranslateSchemaText, preTranslateSchemaText);
            // make sure Avro accepts it
            Schema avroSchema = Schema.parse(avroTextFromSchema);
            SchemaParser parser = new SchemaParser();
            ValidationOptions options = new ValidationOptions();
            options.setAvroUnionMode(true);
            parser.setValidationOptions(options);
            parser.parse(avroTextFromSchema);
            assertFalse(parser.hasError(), parser.errorMessage());
            if (optionalDefaultMode == DataToAvroSchemaTranslationOptions.DEFAULT_OPTIONAL_DEFAULT_MODE) {
                // use other dataToAvroSchemaJson
                String avroSchema2Json = SchemaTranslator.dataToAvroSchemaJson(TestUtil.dataSchemaFromString(schemaText));
                String avroSchema2JsonCompact = SchemaTranslator.dataToAvroSchemaJson(TestUtil.dataSchemaFromString(schemaText), new DataToAvroSchemaTranslationOptions());
                assertEquals(avroSchema2Json, avroSchema2JsonCompact);
                Schema avroSchema2 = Schema.parse(avroSchema2Json);
                assertEquals(avroSchema2, avroSchema);
                // use dataToAvroSchema
                Schema avroSchema3 = SchemaTranslator.dataToAvroSchema(TestUtil.dataSchemaFromString(schemaText));
                assertEquals(avroSchema3, avroSchema2);
            }
            if (writerSchemaText != null || avroValueJson != null) {
                // check if the translated default value is good by using it.
                // writer schema and Avro JSON value should not include fields with default values.
                Schema writerSchema = Schema.parse(writerSchemaText);
                GenericRecord genericRecord = genericRecordFromString(avroValueJson, writerSchema, avroSchema);
                if (expectedGenericRecordJson != null) {
                    String genericRecordAsString = genericRecord.toString();
                    assertEquals(genericRecordAsString, TestAvroUtil.serializedEnumValueProcessor(expectedGenericRecordJson));
                }
            }
            if (embedSchemaMode == EmbedSchemaMode.ROOT_ONLY && hasEmbeddedSchema(schema)) {
                // if embedded schema is enabled, translate Avro back to Pegasus schema.
                // the output Pegasus schema should be exactly same the input schema
                // taking into account typeref.
                AvroToDataSchemaTranslationOptions avroToDataSchemaMode = new AvroToDataSchemaTranslationOptions(AvroToDataSchemaTranslationMode.VERIFY_EMBEDDED_SCHEMA);
                DataSchema embeddedSchema = SchemaTranslator.avroToDataSchema(avroTextFromSchema, avroToDataSchemaMode);
                assertEquals(embeddedSchema, schema.getDereferencedDataSchema());
            }
        }
    }
}
Also used : PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) DataSchema(com.linkedin.data.schema.DataSchema) Schema(org.apache.avro.Schema) SchemaParser(com.linkedin.data.schema.SchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) DataMap(com.linkedin.data.DataMap) DataSchema(com.linkedin.data.schema.DataSchema) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 4 with PegasusSchemaParser

use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.

the class TestUtil method schemaParserFromObjects.

public static PegasusSchemaParser schemaParserFromObjects(List<Object> objects) throws IOException {
    SchemaParser parser = new SchemaParser();
    parser.parse(objects);
    return parser;
}
Also used : SchemaParser(com.linkedin.data.schema.SchemaParser) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser)

Example 5 with PegasusSchemaParser

use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.

the class TestUtil method schemaParserFromString.

public static PegasusSchemaParser schemaParserFromString(String s) throws UnsupportedEncodingException, IOException {
    PegasusSchemaParser parser = new SchemaParser();
    parser.parse(inputStreamFromString(s));
    return parser;
}
Also used : PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) SchemaParser(com.linkedin.data.schema.SchemaParser) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser)

Aggregations

PegasusSchemaParser (com.linkedin.data.schema.PegasusSchemaParser)17 DataSchema (com.linkedin.data.schema.DataSchema)11 SchemaParser (com.linkedin.data.schema.SchemaParser)11 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)7 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)7 PdlSchemaParser (com.linkedin.data.schema.grammar.PdlSchemaParser)6 DataMap (com.linkedin.data.DataMap)5 Schema (org.apache.avro.Schema)4 Test (org.testng.annotations.Test)4 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 BeforeTest (org.testng.annotations.BeforeTest)3 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)2 ArrayList (java.util.ArrayList)2 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)1 DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)1 DataSchemaTraverse (com.linkedin.data.schema.DataSchemaTraverse)1 SchemaParserFactory (com.linkedin.data.schema.SchemaParserFactory)1 DefaultDataSchemaResolver (com.linkedin.data.schema.resolver.DefaultDataSchemaResolver)1 FileDataSchemaLocation (com.linkedin.data.schema.resolver.FileDataSchemaLocation)1