@Override public Schema copy() throws CopyNotSupportedException { return new Schema.Parser().setValidate(false).parse(this.schema.toString()); } }
/** Construct a schema from <a href="http://json.org/">JSON</a> text. * @param validate true if names should be validated, false if not. * @deprecated use {@link Schema.Parser} instead. */ public static Schema parse(String jsonSchema, boolean validate) { return new Parser().setValidate(validate).parse(jsonSchema); }
/** Construct a schema from <a href="http://json.org/">JSON</a> text. * @param validate true if names should be validated, false if not. * @deprecated use {@link Schema.Parser} instead. */ public static Schema parse(String jsonSchema, boolean validate) { return new Parser().setValidate(validate).parse(jsonSchema); }
/** * This routine might try to parse the schema twice. * * @param schemaStr * @return */ public static Schema parseSchema(String schemaStr, Configuration conf) { Schema schema = null; try { schema = new Schema.Parser().parse(schemaStr); } catch (SchemaParseException ex) { boolean skipNameValidation = skipNameValidation(conf); if (skipNameValidation) { LOG.warn("Cannot parse schema. " + ex); LOG.info("Try one more time without name validation."); Schema.Parser parser = new Schema.Parser(); schema = parser.setValidate(false).parse(schemaStr); } else { throw ex; } } return schema; }
/** * Method for reading an Avro Schema from given {@link InputStream}, * and once done (successfully or not), closing the stream. * * @since 2.6 */ public AvroSchema schemaFrom(InputStream in) throws IOException { try { return new AvroSchema(new Schema.Parser().setValidate(true) .parse(in)); } finally { in.close(); } }
/** * Convenience method for reading {@link AvroSchema} from given * encoded JSON representation. * * @since 2.6 */ public AvroSchema schemaFrom(File schemaFile) throws IOException { return new AvroSchema(new Schema.Parser().setValidate(true) .parse(schemaFile)); } }
/** * Convenience method for reading {@link AvroSchema} from given * encoded JSON representation. * * @since 2.6 */ public AvroSchema schemaFrom(String schemaAsString) throws IOException { return new AvroSchema(new Schema.Parser().setValidate(true) .parse(schemaAsString)); }
/** Construct a schema from <a href="http://json.org/">JSON</a> text. * @param validate true if names should be validated, false if not. * @deprecated use {@link Schema.Parser} instead. */ public static Schema parse(String jsonSchema, boolean validate) { return new Parser().setValidate(validate).parse(jsonSchema); }
@Override public Schema copy() throws CopyNotSupportedException { return new Schema.Parser().setValidate(false).parse(this.schema.toString()); } }
/** Construct a schema from <a href="http://json.org/">JSON</a> text. * @param validate true if names should be validated, false if not. * @deprecated use {@link Schema.Parser} instead. */ public static Schema parse(String jsonSchema, boolean validate) { return new Parser().setValidate(validate).parse(jsonSchema); }
@Override public Schema deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException { Parser schemaParser = new Schema.Parser(); // Validate any default values provided schemaParser.setValidateDefaults(true); // Validate all names. schemaParser.setValidate(true); return schemaParser.parse(parser.readValueAsTree().toString()); }
@Override protected Collection<ValidationResult> customValidate(ValidationContext validationContext) { Set<ValidationResult> results = new HashSet<>(); boolean strict = validationContext.getProperty(VALIDATE_FIELD_NAMES).asBoolean(); // Iterate over dynamic properties, validating the schemas, and adding results validationContext.getProperties().entrySet().stream().filter(entry -> entry.getKey().isDynamic()).forEach(entry -> { String subject = entry.getKey().getDisplayName(); String input = entry.getValue(); try { final Schema avroSchema = new Schema.Parser().setValidate(strict).parse(input); AvroTypeUtil.createSchema(avroSchema, input, SchemaIdentifier.EMPTY); } catch (final Exception e) { results.add(new ValidationResult.Builder() .input(input) .subject(subject) .valid(false) .explanation("Not a valid Avro Schema: " + e.getMessage()) .build()); } }); return results; }
@Override public void onPropertyModified(final PropertyDescriptor descriptor, final String oldValue, final String newValue) { if(descriptor.isDynamic()) { // Dynamic property = schema, validate it if (newValue == null) { recordSchemas.remove(descriptor.getName()); } else { try { // Use a non-strict parser here, a strict parse can be done (if specified) in customValidate(). final Schema avroSchema = new Schema.Parser().setValidate(false).parse(newValue); final SchemaIdentifier schemaId = SchemaIdentifier.builder().name(descriptor.getName()).build(); final RecordSchema recordSchema = AvroTypeUtil.createSchema(avroSchema, newValue, schemaId); recordSchemas.put(descriptor.getName(), recordSchema); } catch (final Exception e) { // not a problem - the service won't be valid and the validation message will indicate what is wrong. } } } }