Search in sources :

Example 61 with RecordDataSchema

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

the class ExtensionSchemaValidationCmdLineApp method parseAndValidateExtensionSchemas.

static void parseAndValidateExtensionSchemas(String resolverPath, File inputDir) throws IOException, InvalidExtensionSchemaException {
    // Parse each extension schema and validate it
    Iterator<File> iterator = FileUtils.iterateFiles(inputDir, new String[] { PDL }, true);
    DataSchemaResolver resolver = MultiFormatDataSchemaResolver.withBuiltinFormats(resolverPath);
    while (iterator.hasNext()) {
        File inputFile = iterator.next();
        PdlSchemaParser parser = new PdlSchemaParser(resolver);
        parser.parse(new FileInputStream(inputFile));
        if (parser.hasError()) {
            throw new InvalidExtensionSchemaException(parser.errorMessage());
        }
        List<DataSchema> topLevelDataSchemas = parser.topLevelDataSchemas();
        if (topLevelDataSchemas == null || topLevelDataSchemas.isEmpty() || topLevelDataSchemas.size() > 1) {
            throw new InvalidExtensionSchemaException("Could not parse extension schema : " + inputFile.getAbsolutePath());
        }
        DataSchema topLevelDataSchema = topLevelDataSchemas.get(0);
        if (!(topLevelDataSchema instanceof NamedDataSchema)) {
            throw new InvalidExtensionSchemaException("Invalid extension schema : " + inputFile.getAbsolutePath() + ", the schema is not a named schema.");
        }
        if (!((NamedDataSchema) topLevelDataSchema).getName().endsWith(EXTENSIONS_SUFFIX)) {
            throw new InvalidExtensionSchemaException("Invalid extension schema name: '" + ((NamedDataSchema) topLevelDataSchema).getName() + "'. The name of the extension schema must be <baseSchemaName> + 'Extensions'");
        }
        List<NamedDataSchema> includes = ((RecordDataSchema) topLevelDataSchema).getInclude();
        if (includes.size() != 1) {
            throw new InvalidExtensionSchemaException("The extension schema: '" + ((NamedDataSchema) topLevelDataSchema).getName() + "' should include and only include the base schema");
        }
        NamedDataSchema includeSchema = includes.get(0);
        if (!((NamedDataSchema) topLevelDataSchema).getName().startsWith(includeSchema.getName())) {
            throw new InvalidExtensionSchemaException("Invalid extension schema name: '" + ((NamedDataSchema) topLevelDataSchema).getName() + "'. The name of the extension schema must be baseSchemaName: '" + includeSchema.getName() + "' + 'Extensions");
        }
        List<RecordDataSchema.Field> extensionSchemaFields = ((RecordDataSchema) topLevelDataSchema).getFields().stream().filter(f -> !((RecordDataSchema) topLevelDataSchema).isFieldFromIncludes(f)).collect(Collectors.toList());
        checkExtensionSchemaFields(extensionSchemaFields);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) Options(org.apache.commons.cli.Options) LoggerFactory(org.slf4j.LoggerFactory) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) HelpFormatter(org.apache.commons.cli.HelpFormatter) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) GnuParser(org.apache.commons.cli.GnuParser) DataMap(com.linkedin.data.DataMap) RequiredMode(com.linkedin.data.schema.validation.RequiredMode) CoercionMode(com.linkedin.data.schema.validation.CoercionMode) ExtensionSchemaAnnotation(com.linkedin.restli.common.ExtensionSchemaAnnotation) Map(java.util.Map) OptionBuilder(org.apache.commons.cli.OptionBuilder) CommandLine(org.apache.commons.cli.CommandLine) EXTENSION_ANNOTATION_NAMESPACE(com.linkedin.data.schema.annotation.ExtensionSchemaAnnotationHandler.EXTENSION_ANNOTATION_NAMESPACE) ValidateDataAgainstSchema(com.linkedin.data.schema.validation.ValidateDataAgainstSchema) MultiFormatDataSchemaResolver(com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) CommandLineParser(org.apache.commons.cli.CommandLineParser) DataList(com.linkedin.data.DataList) UnrecognizedFieldMode(com.linkedin.data.schema.validation.UnrecognizedFieldMode) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) RestLiToolsUtils(com.linkedin.restli.internal.tools.RestLiToolsUtils) ParseException(org.apache.commons.cli.ParseException) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) FileInputStream(java.io.FileInputStream) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) MultiFormatDataSchemaResolver(com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) File(java.io.File) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser)

Example 62 with RecordDataSchema

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

the class TestTemplateSpecGenerator method testCustomInfoForRecordFields.

@Test(dataProvider = "customTypeDataForRecord")
public void testCustomInfoForRecordFields(final List<DataSchema> customTypedSchemas) {
    final List<RecordDataSchema.Field> fields = customTypedSchemas.stream().map(RecordDataSchema.Field::new).peek(field -> field.setName("field_" + _uniqueNumberGenerator.getAndIncrement(), null)).collect(Collectors.toList());
    final RecordDataSchema record = new RecordDataSchema(new Name(INPUT_SCHEMA_NAME), RecordDataSchema.RecordType.RECORD);
    record.setFields(fields, null);
    final TemplateSpecGenerator generator = new TemplateSpecGenerator(_resolver);
    final RecordTemplateSpec spec = (RecordTemplateSpec) generator.generate(record, _location);
    for (int i = 0; i < customTypedSchemas.size(); ++i) {
        Assert.assertNotNull(spec.getFields().get(i).getCustomInfo());
        Assert.assertEquals(spec.getFields().get(i).getCustomInfo().getCustomClass().getClassName(), CustomTypeUtil.getJavaCustomTypeClassNameFromSchema((TyperefDataSchema) customTypedSchemas.get(i)));
    }
}
Also used : Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) Mock(org.mockito.Mock) DataSchema(com.linkedin.data.schema.DataSchema) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) StringDataSchemaLocation(com.linkedin.data.schema.StringDataSchemaLocation) CustomTypeUtil(com.linkedin.util.CustomTypeUtil) ArrayList(java.util.ArrayList) MockitoAnnotations(org.mockito.MockitoAnnotations) DataMap(com.linkedin.data.DataMap) Assert(org.testng.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) BeforeMethod(org.testng.annotations.BeforeMethod) Collectors(java.util.stream.Collectors) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) Mockito(org.mockito.Mockito) List(java.util.List) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) DataSchemaUtil(com.linkedin.data.schema.DataSchemaUtil) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec) Collections(java.util.Collections) Name(com.linkedin.data.schema.Name) RecordTemplateSpec(com.linkedin.pegasus.generator.spec.RecordTemplateSpec) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) RecordTemplateSpec(com.linkedin.pegasus.generator.spec.RecordTemplateSpec) Name(com.linkedin.data.schema.Name) Test(org.testng.annotations.Test)

Example 63 with RecordDataSchema

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

the class RestLiResponseException method getErrorDetailsRecord.

/**
 * Gets the error details as a typed record based on the error detail type. {@code null} will be returned if
 * there are no error details, if there is no error detail type, or if no class is found that corresponds with
 * the error detail type.
 *
 * @param <T> the error detail type specified in the {@link ErrorResponse}
 * @return the error details as a typed record, or null
 */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
public <T extends RecordTemplate> T getErrorDetailsRecord() {
    if (_errorResponse.hasErrorDetails() && _errorResponse.hasErrorDetailType()) {
        String type = _errorResponse.getErrorDetailType();
        try {
            Class<?> typeClass = Class.forName(type);
            if (RecordTemplate.class.isAssignableFrom(typeClass)) {
                Class<? extends RecordTemplate> recordType = typeClass.asSubclass(RecordTemplate.class);
                RecordDataSchema schema = (RecordDataSchema) DataTemplateUtil.getSchema(typeClass);
                return (T) DataTemplateUtil.wrap(_errorResponse.getErrorDetails().data(), schema, recordType);
            }
        } catch (ClassNotFoundException e) {
            return null;
        }
    }
    return null;
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Example 64 with RecordDataSchema

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

the class GetResponseBuilder method buildRestLiResponseData.

/**
 * {@inheritDoc}
 *
 * @param result The result of a Rest.li GET method. It can be the entity itself, or the entity wrapped in a
 *               {@link GetResult}.
 */
@Override
public RestLiResponseData<GetResponseEnvelope> buildRestLiResponseData(Request request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    final RecordTemplate record;
    final HttpStatus status;
    if (result instanceof GetResult) {
        final GetResult<?> getResult = (GetResult<?>) result;
        record = getResult.getValue();
        status = getResult.getStatus();
    } else {
        record = (RecordTemplate) result;
        status = HttpStatus.S_200_OK;
    }
    final ResourceContext resourceContext = routingResult.getContext();
    DataMap rawData = record.data();
    RecordDataSchema schema = record.schema();
    if (resourceContext.isFillInDefaultsRequested()) {
        rawData = (DataMap) ResponseUtils.fillInDataDefault(schema, rawData);
    }
    TimingContextUtil.beginTiming(resourceContext.getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
    final DataMap data = RestUtils.projectFields(rawData, resourceContext);
    TimingContextUtil.endTiming(resourceContext.getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
    return new RestLiResponseDataImpl<>(new GetResponseEnvelope(status, new AnyRecord(data)), headers, cookies);
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) GetResult(com.linkedin.restli.server.GetResult) HttpStatus(com.linkedin.restli.common.HttpStatus) RecordTemplate(com.linkedin.data.template.RecordTemplate) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap)

Example 65 with RecordDataSchema

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

the class ResponseUtils method fillInDefaultOnRecord.

private static DataMap fillInDefaultOnRecord(RecordDataSchema schema, DataMap dataMap) throws CloneNotSupportedException {
    DataMap dataWithDefault = dataMap.clone();
    for (RecordDataSchema.Field field : schema.getFields()) {
        if (dataMap.containsKey(field.getName()) || field.getDefault() != null) {
            Object fieldData = dataMap.containsKey(field.getName()) ? dataMap.get(field.getName()) : field.getDefault();
            CheckedUtil.putWithoutChecking(dataWithDefault, field.getName(), fillInDataDefault(field.getType(), fieldData));
        }
    }
    return dataWithDefault;
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap)

Aggregations

RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)106 DataMap (com.linkedin.data.DataMap)43 Test (org.testng.annotations.Test)43 DataSchema (com.linkedin.data.schema.DataSchema)40 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)32 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)28 MapDataSchema (com.linkedin.data.schema.MapDataSchema)26 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)25 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)20 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)19 ArrayList (java.util.ArrayList)18 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)14 Name (com.linkedin.data.schema.Name)14 Map (java.util.Map)10 Schema (org.apache.avro.Schema)10 DataList (com.linkedin.data.DataList)9 GenericRecord (org.apache.avro.generic.GenericRecord)9 ValidateDataAgainstSchema (com.linkedin.data.schema.validation.ValidateDataAgainstSchema)8 HashMap (java.util.HashMap)8 FieldDef (com.linkedin.data.template.FieldDef)7