Skip to content

Commit

Permalink
feat: no-file-descriptor setting for outputSchema option (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukealvoeiro authored Jun 7, 2024
1 parent 58deee2 commit c54f06c
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ Generated code will be placed in the Gradle build directory.

- With `--ts_proto_opt=annotateFilesWithVersion=false`, the generated files will not contain the versions of `protoc` and `ts-proto` used to generate the file. This option is normally set to `true`, such that files list the versions used.

- With `--ts_proto_opt=outputSchema=true`, meta typings will be generated that can later be used in other code generators.
- With `--ts_proto_opt=outputSchema=true`, meta typings will be generated that can later be used in other code generators. If outputSchema is instead specified to be `no-file-descriptor` then we do not include the file descriptor in the generated schema. This is useful if you are trying to minimize the size of the generated schema.

- With `--ts_proto_opt=outputTypeAnnotations=true`, each message will be given a `$type` field containing its fully-qualified name. You can use `--ts_proto_opt=outputTypeAnnotations=static-only` to omit it from the `interface` declaration, or `--ts_proto_opt=outputTypeAnnotations=optional` to make it an optional property on the `interface` definition. The latter option may be useful if you want to use the `$type` field for runtime type checking on responses from a server.

Expand Down
Binary file added integration/schema-no-file-descriptor/const-enum.bin
Binary file not shown.
13 changes: 13 additions & 0 deletions integration/schema-no-file-descriptor/const-enum.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

message DividerData {
enum DividerType {
DOUBLE = 0;
SINGLE = 1;
DASHED = 2;
DOTTED = 3;
}

DividerType type = 1;
map<string, DividerType> typeMap = 2;
}
281 changes: 281 additions & 0 deletions integration/schema-no-file-descriptor/const-enum.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions integration/schema-no-file-descriptor/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outputSchema=no-file-descriptor
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { protoMetadata } from "./const-enum";

describe("schema-no-file-descriptor", () => {
test("the property doesn't exist", () => {
expect(protoMetadata).not.toHaveProperty("fileDescriptor");
expect(protoMetadata).toHaveProperty("references");
});
});
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export type Options = {
unrecognizedEnumName: string;
unrecognizedEnumValue: number;
exportCommonSymbols: boolean;
outputSchema: boolean;
outputSchema: boolean | "no-file-descriptor";
onlyTypes: boolean;
emitImportedFiles: boolean;
useAbortSignal: boolean;
Expand Down
12 changes: 8 additions & 4 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ export function generateSchema(ctx: Context, fileDesc: FileDescriptorProto, sour
};
export interface ProtoMetadata {
fileDescriptor: ${fileDescriptorProto};
references: { [key: string]: any };
${
ctx.options.outputSchema !== "no-file-descriptor" ? code`fileDescriptor: ${fileDescriptorProto};\n` : ""
}references: { [key: string]: any };
dependencies?: ProtoMetadata[];
options?: {
options?: { [key: string]: any };
Expand Down Expand Up @@ -181,8 +182,11 @@ export function generateSchema(ctx: Context, fileDesc: FileDescriptorProto, sour

chunks.push(code`
export const ${def("protoMetadata")}: ProtoMetadata = {
fileDescriptor: ${fileDescriptorProto}.fromPartial(${descriptor}),
references: { ${joinCode(references, { on: "," })} },
${
ctx.options.outputSchema !== "no-file-descriptor"
? code`fileDescriptor: ${fileDescriptorProto}.fromPartial(${descriptor}),\n`
: ""
}references: { ${joinCode(references, { on: "," })} },
dependencies: [${joinCode(dependencies, { on: "," })}],
${
fileOptions || messagesOptions.length > 0 || servicesOptions.length > 0 || enumsOptions.length > 0
Expand Down

0 comments on commit c54f06c

Please sign in to comment.