Skip to content

Commit

Permalink
feat(csharp): Support HTTP endpoints in GRPC service (#5104)
Browse files Browse the repository at this point in the history
feat(csharp): Support HTTP endpoints in GRPC service
  • Loading branch information
Swimburger authored Nov 6, 2024
1 parent 7e101bf commit cd1aac6
Show file tree
Hide file tree
Showing 199 changed files with 10,571 additions and 784 deletions.
2 changes: 1 addition & 1 deletion generators/csharp/codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@fern-api/fs-utils": "workspace:*",
"@fern-api/generator-commons": "workspace:*",
"@fern-api/logging-execa": "workspace:*",
"@fern-fern/ir-sdk": "^53.7.0",
"@fern-fern/ir-sdk": "^53.18.0",
"lodash-es": "^4.17.21",
"zod": "^3.22.3"
},
Expand Down
2 changes: 1 addition & 1 deletion generators/csharp/model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@fern-api/csharp-codegen": "workspace:*",
"@fern-api/fs-utils": "workspace:*",
"@fern-api/generator-commons": "workspace:*",
"@fern-fern/ir-sdk": "^53.7.0",
"@fern-fern/ir-sdk": "^53.18.0",
"zod": "^3.22.3",
"@types/jest": "^29.5.12",
"@types/node": "18.7.18",
Expand Down
2 changes: 1 addition & 1 deletion generators/csharp/model/src/snippets/ExampleGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export class ExampleGenerator {
double: (p) => csharp.InstantiatedPrimitive.double(p),
boolean: (p) => csharp.InstantiatedPrimitive.boolean(p),
string: (p) => csharp.InstantiatedPrimitive.string(p.original),
datetime: (datetime) => csharp.InstantiatedPrimitive.dateTime(datetime, parseDatetimes),
datetime: (example) => csharp.InstantiatedPrimitive.dateTime(example.datetime, parseDatetimes),
date: (dateString) => csharp.InstantiatedPrimitive.date(dateString),
uuid: (p) => csharp.InstantiatedPrimitive.uuid(p),
base64: (p) => csharp.InstantiatedPrimitive.string(p),
Expand Down
2 changes: 1 addition & 1 deletion generators/csharp/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@fern-api/logger": "workspace:*",
"@fern-fern/generator-cli-sdk": "0.0.17",
"@fern-fern/generator-exec-sdk": "^0.0.898",
"@fern-fern/ir-sdk": "^53.7.0",
"@fern-fern/ir-sdk": "^53.18.0",
"lodash-es": "^4.17.21",
"url-join": "^5.0.0",
"zod": "^3.22.3",
Expand Down
4 changes: 4 additions & 0 deletions generators/csharp/sdk/src/SdkGeneratorContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ export class SdkGeneratorContext extends AbstractCsharpGeneratorContext<SdkCusto
return `${endpoint.name.pascalCase.safeName}Async`;
}

public endpointUsesGrpcTransport(service: HttpService, endpoint: HttpEndpoint): boolean {
return service.transport?.type === "grpc" && endpoint.transport?.type !== "http";
}

public getExtraDependencies(): Record<string, string> {
return this.customConfig["extra-dependencies"] ?? {};
}
Expand Down
4 changes: 3 additions & 1 deletion generators/csharp/sdk/src/endpoint/EndpointGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export class EndpointGenerator extends AbstractEndpointGenerator {
rawClient: RawClient;
grpcClientInfo: GrpcClientInfo | undefined;
}): csharp.Method {
if (grpcClientInfo != null) {
// If the service is a grpc service, grpcClientInfo will not be null or undefined,
// so any endpoint will be generated as a grpc endpoint, unless the transport is overriden by setting type to http
if (grpcClientInfo != null && endpoint.transport?.type !== "http") {
return this.grpc.generate({
serviceId,
endpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export class WrappedRequestGenerator extends FileGenerator<CSharpFile, SdkCustom
type: csharp.Class.ClassType.Record
});

const service = this.context.getHttpServiceOrThrow(this.serviceId);
const isProtoRequest = this.context.endpointUsesGrpcTransport(service, this.endpoint);
const protobufProperties: { propertyName: string; typeReference: TypeReference }[] = [];
for (const query of this.endpoint.queryParameters) {
const propertyName = query.name.name.pascalCase.safeName;
Expand All @@ -67,14 +69,15 @@ export class WrappedRequestGenerator extends FileGenerator<CSharpFile, SdkCustom
})
);

protobufProperties.push({
propertyName,
typeReference: query.allowMultiple
? TypeReference.container(ContainerType.list(query.valueType))
: query.valueType
});
if (isProtoRequest) {
protobufProperties.push({
propertyName,
typeReference: query.allowMultiple
? TypeReference.container(ContainerType.list(query.valueType))
: query.valueType
});
}
}
const service = this.context.getHttpServiceOrThrow(this.serviceId);
for (const header of [...service.headers, ...this.endpoint.headers]) {
class_.addField(
csharp.field({
Expand Down Expand Up @@ -121,10 +124,12 @@ export class WrappedRequestGenerator extends FileGenerator<CSharpFile, SdkCustom
})
);

protobufProperties.push({
propertyName,
typeReference: property.valueType
});
if (isProtoRequest) {
protobufProperties.push({
propertyName,
typeReference: property.valueType
});
}
}
},
fileUpload: () => undefined,
Expand All @@ -134,20 +139,22 @@ export class WrappedRequestGenerator extends FileGenerator<CSharpFile, SdkCustom

class_.addMethod(this.context.getToStringMethod());

const protobufService = this.context.protobufResolver.getProtobufServiceForServiceId(this.serviceId);
if (protobufService != null) {
const protobufClassReference = new csharp.ClassReference({
name: this.classReference.name,
namespace: this.context.protobufResolver.getNamespaceFromProtobufFileOrThrow(protobufService.file),
namespaceAlias: "Proto"
});
class_.addMethod(
this.context.csharpProtobufTypeMapper.toProtoMethod({
classReference: this.classReference,
protobufClassReference,
properties: protobufProperties
})
);
if (isProtoRequest) {
const protobufService = this.context.protobufResolver.getProtobufServiceForServiceId(this.serviceId);
if (protobufService != null) {
const protobufClassReference = new csharp.ClassReference({
name: this.classReference.name,
namespace: this.context.protobufResolver.getNamespaceFromProtobufFileOrThrow(protobufService.file),
namespaceAlias: "Proto"
});
class_.addMethod(
this.context.csharpProtobufTypeMapper.toProtoMethod({
classReference: this.classReference,
protobufClassReference,
properties: protobufProperties
})
);
}
}

return new CSharpFile({
Expand Down
7 changes: 7 additions & 0 deletions generators/csharp/sdk/versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
# The C# SDK now uses forward-compatible enums which are not compatible with the previously generated enums.
# Set `enable-forward-compatible-enums` to `false` in the configuration to generate the old enums.
# irVersion: 53
- version: 1.9.0
createdAt: "2024-11-05"
changelogEntry:
- type: feat
summary: >-
Add support for calling HTTP endpoints and gRPC endoints within the same service.
irVersion: 53
- version: 1.8.5
createdAt: "2024-10-30"
changelogEntry:
Expand Down
24 changes: 11 additions & 13 deletions pnpm-lock.yaml

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

Loading

0 comments on commit cd1aac6

Please sign in to comment.