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

Name the plain type which actually matches the protobuf definition #1060

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

abalmos
Copy link

@abalmos abalmos commented Jan 25, 2025

The type was already generated, but not exported (except as a union export). Having access to this plain type is almost essential for application code that utilizes the generated message types for purposes beyond this library's own API.

fixes #1058

The type was already generated but not exported (except as a union export).
Having access to this plain type is almost essential for application code that
utilizes the generated message types for purposes beyond this library's own API.

Signed-off-by: Andrew Balmos <[email protected]>
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@anthonysessa
Copy link

anthonysessa commented Jan 30, 2025

I think the issue here is that oneOf messages create a nested structure that utilizes a case: value approach. So the end result in this case is not representative of the basic proto typings.

Example:

syntax = "proto3";

import "google/protobuf/timestamp.proto";

message Pet {
  string name = 1;
  PetType pet_type = 2;
}

message PetType {
  oneof type {
    Cat cat = 1;
    Dog dog = 2;
  }
}

message Cat {
  string breed = 1;
}

message Dog {
  string breed = 1;
}

Generates:

// @generated by protoc-gen-es v2.2.3 with parameter "json_types=true"
// @generated from file com/clearme/protobuf/Pet.proto (syntax proto3)
/* eslint-disable */

import type { GenFile, GenMessage } from "@bufbuild/protobuf/codegenv1";
import type { Message } from "@bufbuild/protobuf";

/**
 * Describes the file com/clearme/protobuf/Pet.proto.
 */
export declare const file_com_clearme_protobuf_Pet: GenFile;

/**
 * @generated from message Pet
 */
export declare type Pet = Message<"Pet"> & {
  /**
   * @generated from field: string name = 1;
   */
  name: string;

  /**
   * @generated from field: PetType pet_type = 2;
   */
  petType?: PetType;
};

/**
 * @generated from message Pet
 */
export declare type PetJson = {
  /**
   * @generated from field: string name = 1;
   */
  name?: string;

  /**
   * @generated from field: PetType pet_type = 2;
   */
  petType?: PetTypeJson;
};

/**
 * Describes the message Pet.
 * Use `create(PetSchema)` to create a new message.
 */
export declare const PetSchema: GenMessage<Pet, PetJson>;

/**
 * @generated from message PetType
 */
export declare type PetType = Message<"PetType"> & {
  /**
   * @generated from oneof PetType.type
   */
  type: {
    /**
     * @generated from field: Cat cat = 1;
     */
    value: Cat;
    case: "cat";
  } | {
    /**
     * @generated from field: Dog dog = 2;
     */
    value: Dog;
    case: "dog";
  } | { case: undefined; value?: undefined };
};

/**
 * @generated from message PetType
 */
export declare type PetTypeJson = {
  /**
   * @generated from field: Cat cat = 1;
   */
  cat?: CatJson;

  /**
   * @generated from field: Dog dog = 2;
   */
  dog?: DogJson;
};

/**
 * Describes the message PetType.
 * Use `create(PetTypeSchema)` to create a new message.
 */
export declare const PetTypeSchema: GenMessage<PetType, PetTypeJson>;

/**
 * @generated from message Cat
 */
export declare type Cat = Message<"Cat"> & {
  /**
   * @generated from field: string breed = 1;
   */
  breed: string;
};

/**
 * @generated from message Cat
 */
export declare type CatJson = {
  /**
   * @generated from field: string breed = 1;
   */
  breed?: string;
};

/**
 * Describes the message Cat.
 * Use `create(CatSchema)` to create a new message.
 */
export declare const CatSchema: GenMessage<Cat, CatJson>;

/**
 * @generated from message Dog
 */
export declare type Dog = Message<"Dog"> & {
  /**
   * @generated from field: string breed = 1;
   */
  breed: string;
};

/**
 * @generated from message Dog
 */
export declare type DogJson = {
  /**
   * @generated from field: string breed = 1;
   */
  breed?: string;
};

/**
 * Describes the message Dog.
 * Use `create(DogSchema)` to create a new message.
 */
export declare const DogSchema: GenMessage<Dog, DogJson>;

I am aiming to get something very similar here: #1062. My suggestion is to add an option to make the json_types respect optional vs. non-optional fields like the main proto types do.

OR

Should json_types respect optional vs non-optional proto definitions out of the box?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create raw data types as well
3 participants