Skip to content

protobuf emitter maps unknown to Any (should have an option for Struct) #11764

Description

@DaveB93

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:

  1. Map unknown to google.protobuf.Value (or Struct for object-shaped data) by default; or
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions