Our TypeSpec has several free form "arbitrary JSON object" fields authored as plain YAML maps. they're typed unknown in TypeSpec, but the emitter renders them as google.protobuf.Any. Any requires a @type URL to be set in the JSON representation so a plain YAML object like {foo: 1} cannot be parsed into the field. Arbitrary JSON should map to google.protobuf.Struct or google.protobuf.Value.
it would be nice if there were decorators to determine what protobuf type is generated here.
Playground Link
Minimal playground spec
import "@typespec/protobuf";
using TypeSpec.Protobuf;
@package({
name: "repro",
})
namespace Repro;
model Thing {
@field(1)
name: string;
// Free-form, arbitrary JSON object authored as a plain YAML/JSON map, e.g. { foo: 1 }.
// We want this to be google.protobuf.Struct in the emitted proto.
@field(2)
arguments?: unknown;
}
Actual emitter output
syntax = "proto3";
package repro;
import "google/protobuf/any.proto";
message Thing {
string name = 1;
google.protobuf.Any arguments = 2;
}
Expected / desired output
syntax = "proto3";
package repro;
import "google/protobuf/struct.proto";
message Thing {
string name = 1;
google.protobuf.Struct arguments = 2;
}
Ask
unknown represents "any JSON value", which round-trips through google.protobuf.Struct
(object) / google.protobuf.Value (any value) - not google.protobuf.Any, whose JSON form
requires a @type URL. Options for the emitter:
- Map
unknown to google.protobuf.Value (or Struct for object-shaped data) by default; or
- Provide an opt-in decorator / config to choose the well-known mapping (e.g.
@TypeSpec.Protobuf.wellKnownType(Struct)).
Either removes the need for downstream post-processing of the generated .proto.
Our TypeSpec has several free form "arbitrary JSON object" fields authored as plain YAML maps. they're typed
unknownin TypeSpec, but the emitter renders them asgoogle.protobuf.Any.Anyrequires a@typeURL to be set in the JSON representation so a plain YAML object like{foo: 1}cannot be parsed into the field. Arbitrary JSON should map togoogle.protobuf.Structorgoogle.protobuf.Value.it would be nice if there were decorators to determine what protobuf type is generated here.
Playground Link
Minimal playground spec
Actual emitter output
Expected / desired output
Ask
unknownrepresents "any JSON value", which round-trips throughgoogle.protobuf.Struct(object) /
google.protobuf.Value(any value) - notgoogle.protobuf.Any, whose JSON formrequires a
@typeURL. Options for the emitter:unknowntogoogle.protobuf.Value(orStructfor object-shaped data) by default; or@TypeSpec.Protobuf.wellKnownType(Struct)).Either removes the need for downstream post-processing of the generated
.proto.