Skip to content

Commit

Permalink
fix: Allow duplicate numbers in enums when aliases are enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
sporkmonger committed Aug 11, 2023
1 parent c144218 commit 1e75d6e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pbjson-build/src/generator/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::{
use crate::descriptor::{EnumDescriptor, TypePath};
use crate::generator::write_fields_array;
use crate::resolver::Resolver;
use std::collections::HashSet;
use std::io::{Result, Write};

pub fn generate_enum<W: Write>(
Expand All @@ -22,9 +23,13 @@ pub fn generate_enum<W: Write>(
) -> Result<()> {
let rust_type = resolver.rust_type(path);

let mut seen_numbers = HashSet::new();
let variants: Vec<_> = descriptor
.values
.iter()
// Skip duplicates if we've seen the number before
// Protobuf's `allow_alias` option permits duplicates if set
.filter(|variant| seen_numbers.insert(variant.number()))
.map(|variant| {
let variant_name = variant.name.clone().unwrap();
let variant_number = variant.number();
Expand Down
1 change: 1 addition & 0 deletions pbjson-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn main() -> Result<()> {
root.join("syntax3.proto"),
root.join("common.proto"),
root.join("duplicate_name.proto"),
root.join("duplicate_number.proto"),
root.join("escape.proto"),
];

Expand Down
16 changes: 16 additions & 0 deletions pbjson-test/protos/duplicate_number.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto3";

package test.duplicate_number;

message Compressor {
enum CompressionLevel {
option allow_alias = true;

DEFAULT = 0;
BEST_SPEED = 1;
COMPRESSION_LEVEL_1 = 1;
COMPRESSION_LEVEL_2 = 2;
COMPRESSION_LEVEL_3 = 3;
BEST_COMPRESSION = 3;
}
}
5 changes: 5 additions & 0 deletions pbjson-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ pub mod test {
include!(concat!(env!("OUT_DIR"), "/test.duplicate_name.serde.rs"));
}

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

pub mod escape {
include!(concat!(
env!("OUT_DIR"),
Expand Down

0 comments on commit 1e75d6e

Please sign in to comment.