Skip to content

Commit

Permalink
Fix escape for default string (#515)
Browse files Browse the repository at this point in the history
The `default` is already unescaped.
  • Loading branch information
ldm0 authored Aug 3, 2021
1 parent 870b9c3 commit 32bc87c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
5 changes: 1 addition & 4 deletions prost-build/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,7 @@ impl<'a> CodeGenerator<'a> {
};
self.buf.push_str(stripped_prefix);
} else {
// TODO: this is only correct if the Protobuf escaping matches Rust escaping. To be
// safer, we should unescape the Protobuf string and re-escape it with the Rust
// escaping mechanisms.
self.buf.push_str(default);
self.buf.push_str(&default.escape_default().to_string());
}
}

Expand Down
4 changes: 4 additions & 0 deletions tests/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ fn main() {
.compile_protos(&[src.join("deprecated_field.proto")], includes)
.unwrap();

config
.compile_protos(&[src.join("default_string_escape.proto")], includes)
.unwrap();

prost_build::Config::new()
.protoc_arg("--experimental_allow_proto3_optional")
.compile_protos(&[src.join("proto3_presence.proto")], includes)
Expand Down
8 changes: 8 additions & 0 deletions tests/src/default_string_escape.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto2";

package default_string_escape;

message Person {
required string name = 1 [default = "[\"unknown\"]"];
required string age = 2;
}
10 changes: 10 additions & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ pub mod invalid {
}
}

pub mod default_string_escape {
include!(concat!(env!("OUT_DIR"), "/default_string_escape.rs"));
}

use alloc::format;
use alloc::vec::Vec;

Expand Down Expand Up @@ -525,6 +529,12 @@ mod tests {
);
}

#[test]
fn test_default_string_escape() {
let msg = default_string_escape::Person::default();
assert_eq!(msg.name, r#"["unknown"]"#);
}

#[test]
fn test_group() {
// optional group
Expand Down

0 comments on commit 32bc87c

Please sign in to comment.