Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid fq_message_name when appending nested message in empty package #845

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 19 additions & 28 deletions prost-build/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::ascii;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::iter;
use std::ops::Deref;

use itertools::{Either, Itertools};
use log::debug;
Expand All @@ -28,7 +29,7 @@ enum Syntax {

pub struct CodeGenerator<'a> {
config: &'a mut Config,
package: String,
package: Vec<String>,
source_info: Option<SourceCodeInfo>,
syntax: Syntax,
message_graph: &'a MessageGraph,
Expand Down Expand Up @@ -68,7 +69,10 @@ impl<'a> CodeGenerator<'a> {

let mut code_gen = CodeGenerator {
config,
package: file.package.unwrap_or_default(),
package: file
.package
.map(|s| s.split('.').map(ToString::to_string).collect())
.unwrap_or_default(),
source_info,
syntax,
message_graph,
Expand Down Expand Up @@ -120,12 +124,10 @@ impl<'a> CodeGenerator<'a> {
debug!(" message: {:?}", message.name());

let message_name = message.name().to_string();
let fq_message_name = format!(
"{}{}.{}",
if self.package.is_empty() { "" } else { "." },
self.package,
message.name()
);
let fq_message_name = iter::once("")
.chain(self.package.iter().map(Deref::deref))
.chain(iter::once(message.name()))
.join(".");

// Skip external types.
if self.extern_paths.resolve_ident(&fq_message_name).is_some() {
Expand Down Expand Up @@ -644,12 +646,11 @@ impl<'a> CodeGenerator<'a> {
let enum_name = to_upper_camel(proto_enum_name);

let enum_values = &desc.value;
let fq_proto_enum_name = format!(
"{}{}.{}",
if self.package.is_empty() { "" } else { "." },
self.package,
proto_enum_name
);
let fq_proto_enum_name = iter::once("")
.chain(self.package.iter().map(|x| x.deref()))
.chain(iter::once(proto_enum_name))
.join(".");

if self
.extern_paths
.resolve_ident(&fq_proto_enum_name)
Expand Down Expand Up @@ -841,7 +842,7 @@ impl<'a> CodeGenerator<'a> {
let service = Service {
name: to_upper_camel(&name),
proto_name: name,
package: self.package.clone(),
package: self.package.join("."),
comments,
methods,
options: service.options.unwrap_or_default(),
Expand All @@ -867,17 +868,15 @@ impl<'a> CodeGenerator<'a> {
self.buf.push_str(&to_snake(module));
self.buf.push_str(" {\n");

self.package.push('.');
self.package.push_str(module);
self.package.push(module.to_string());

self.depth += 1;
}

fn pop_mod(&mut self) {
self.depth -= 1;

let idx = self.package.rfind('.').unwrap();
self.package.truncate(idx);
self.package.pop();

self.push_indent();
self.buf.push_str("}\n");
Expand Down Expand Up @@ -915,15 +914,7 @@ impl<'a> CodeGenerator<'a> {
return proto_ident;
}

let mut local_path = self.package.split('.').peekable();

// If no package is specified the start of the package name will be '.'
// and split will return an empty string ("") which breaks resolution
// The fix to this is to ignore the first item if it is empty.
if local_path.peek().map_or(false, |s| s.is_empty()) {
local_path.next();
}

let mut local_path = self.package.iter().map(Deref::deref).peekable();
let mut ident_path = pb_ident[1..].split('.');
let ident_type = ident_path.next_back().unwrap();
let mut ident_path = ident_path.peekable();
Expand Down
4 changes: 4 additions & 0 deletions tests/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ fn main() {
.compile_protos(&[src.join("custom_debug.proto")], includes)
.unwrap();

config
.compile_protos(&[src.join("issue_843.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/issue_843.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";

message M {
message SubMessage {
map<string, string> item = 1;
}
SubMessage reply = 2;
}
9 changes: 9 additions & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ pub mod default_string_escape {
include!(concat!(env!("OUT_DIR"), "/default_string_escape.rs"));
}

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

use alloc::vec::Vec;

use anyhow::anyhow;
Expand Down Expand Up @@ -624,6 +628,11 @@ mod tests {
assert_eq!(msg.name, r#"["unknown"]"#);
}

#[test]
fn test_issue_843() {
let _msg = issue_843::M::default();
}

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