[ 
https://issues.apache.org/jira/browse/AVRO-3460?focusedWorklogId=746543&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-746543
 ]

ASF GitHub Bot logged work on AVRO-3460:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 23/Mar/22 12:55
            Start Date: 23/Mar/22 12:55
    Worklog Time Spent: 10m 
      Work Description: martin-g opened a new pull request #1620:
URL: https://github.com/apache/avro/pull/1620


   
   ### Jira
   
   - [X] My PR addresses the following [Avro 
Jira](https://issues.apache.org/jira/browse/AVRO/) issues and references them 
in the PR title. For example, "AVRO-1234: My Avro PR"
     - https://issues.apache.org/jira/browse/AVRO-3460
     - 
   ### Tests
   
   - [X] My PR adds new unit tests
   
   ### Commits
   
   - [X] My commits all reference Jira issues in their subject lines. In 
addition, my commits follow the guidelines from "[How to write a good git 
commit message](https://chris.beams.io/posts/git-commit/)":
     1. Subject is separated from body by a blank line
     1. Subject is limited to 50 characters (not including Jira issue reference)
     1. Subject does not end with a period
     1. Subject uses the imperative mood ("add", not "adding")
     1. Body wraps at 72 characters
     1. Body explains "what" and "why", not "how"
   
   ### Documentation
   
   - [X] No need of new documentation
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

            Worklog Id:     (was: 746543)
    Remaining Estimate: 0h
            Time Spent: 10m

> [rust] Value::validate does not validate against Schema Refs
> ------------------------------------------------------------
>
>                 Key: AVRO-3460
>                 URL: https://issues.apache.org/jira/browse/AVRO-3460
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: rust
>            Reporter: Jack Klamer
>            Assignee: Martin Tzvetanov Grigorov
>            Priority: Major
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> The Value::validate() method is a preprocessing step before encoding the 
> value into the buffer. The encode function assumes the value to be valid. The 
> validate method recurses as necessary to validate against the schema 
> provided. The function does not recurse to validate against schema refs found 
> in the schema provided meaning that all values are validated to true against 
> all schema Ref's which is incorrect.
> {code}
>  pub fn validate(&self, schema: &Schema) -> bool {        
>     match (self, schema) {            
>         (_, &Schema::Ref { name: _ }) => true, 
> ....{code}
> In practice this failure requires a user to forget to change their Schema 
> when changing their struct and that struct change happens to be the second 
> use of a struct within the enclosing struct. (Or fixed or enum).
> Because encode assumes the values passed to it are valid, it is possible for 
> the encoding to complete without error in some situations. 
>  
>  
> Two tests that should pass and currently fail, useful for anyone interested 
> {code}
> #[test]
>     fn test_validation_with_refs() {
>         let schema = Schema::parse_str(
>             r#"
>         {
>             "type":"record",
>             "name":"TestStruct",
>             "fields": [
>                 {
>                     "name":"a",
>                     "type":{
>                         "type":"record",
>                         "name": "Inner",
>                         "fields": [ {
>                             "name":"z",
>                             "type":"int"
>                         }]
>                     }
>                 },
>                 {
>                     "name":"b",
>                     "type":"Inner"
>                 }
>             ]
>         }"#,
>         )
>         .unwrap();
>         let inner_value_right = Value::Record(vec![("z".into(), 
> Value::Int(3))]);
>         let inner_value_wrong1 = Value::Record(vec![("z".into(), 
> Value::Null)]);
>         let inner_value_wrong2 = Value::Record(vec![("a".into(), 
> Value::String("testing".into()))]);
>         let outer1 = Value::Record(vec![
>             ("a".into(), inner_value_right.clone()),
>             ("b".into(), inner_value_wrong1),
>         ]);
>         let outer2 = Value::Record(vec![
>             ("a".into(), inner_value_right),
>             ("b".into(), inner_value_wrong2),
>         ]);
>         assert!(!outer1.validate(&schema), "field b record is invalid against 
> the schema"); // this should pass, but doesn't
>         assert!(!outer2.validate(&schema), "field b record is invalid against 
> the schema"); // this should pass, but doesn't
>     }
>     #[derive(Serialize, Clone)]
>     struct TestInner {
>         z: i32
>     }
>     #[derive(Serialize)]
>     struct TestRefSchemaStruct1 {
>         a: TestInner,
>         b: String // could be literally anything
>     }
>     #[derive(Serialize)]
>     struct TestRefSchemaStruct2 {
>         a: TestInner,
>         b: i32 // could be literally anything
>     }
>     #[derive(Serialize)]
>     struct TestRefSchemaStruct3 {
>         a: TestInner,
>         b: Option<TestInner> // could be literally anything
>     }
>     #[test]
>     fn test_validation_with_refs_real_struct() {
>         let schema = Schema::parse_str(
>             r#"
>         {
>             "type":"record",
>             "name":"TestStruct",
>             "fields": [
>                 {
>                     "name":"a",
>                     "type":{
>                         "type":"record",
>                         "name": "Inner",
>                         "fields": [ {
>                             "name":"z",
>                             "type":"int"
>                         }]
>                     }
>                 },
>                 {
>                     "name":"b",
>                     "type":"Inner"
>                 }
>             ]
>         }"#,
>         )
>         .unwrap();
>         let test_inner  = TestInner{z:3};
>         let test_outer1 = TestRefSchemaStruct1{a:test_inner.clone(), 
> b:"testing".into()};
>         let test_outer2 = TestRefSchemaStruct2{a:test_inner.clone(), b:24};
>         let test_outer3 = TestRefSchemaStruct3{a:test_inner.clone(), b:None};
>         
>         use crate::ser::Serializer;
>         let mut ser =  Serializer::default();
>         let test_outer1: Value = test_outer1.serialize(&mut ser ).unwrap();
>         let mut ser =  Serializer::default(); 
>         let test_outer2: Value = test_outer2.serialize(&mut ser ).unwrap();
>         let mut ser =  Serializer::default(); 
>         let test_outer3: Value = test_outer3.serialize(&mut ser ).unwrap(); 
>         assert!(!test_outer1.validate(&schema), "field b record is invalid 
> against the schema"); // this should pass, but doesn't
>         assert!(!test_outer2.validate(&schema), "field b record is invalid 
> against the schema"); // this should pass, but doesn't
>         assert!(!test_outer3.validate(&schema), "field b record is invalid 
> against the schema"); // this should pass, but doesn't
>     }
> {code}



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to