Skip to content

Commit

Permalink
fix(csharp): literal support in wrapped requests (#5982)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi committed Feb 13, 2025
1 parent 72010d9 commit 484302f
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,23 @@ export abstract class AbstractCsharpGeneratorContext<
/**
* Returns the literal value from a Type Reference (doesn't unbox containers to find a literal).
*/
public getLiteralFromTypeReference({
public getLiteralInitializerFromTypeReference({
typeReference
}: {
typeReference: TypeReference;
}): string | boolean | undefined {
}): csharp.CodeBlock | undefined {
const literalValue = this.getLiteralValue(typeReference);
if (literalValue != null) {
return csharp.codeblock(
typeof literalValue === "boolean"
? `${literalValue.toString().toLowerCase()}`
: `"${literalValue}"`
);
}
return undefined;
}

private getLiteralValue(typeReference: TypeReference): string | boolean | undefined {
if (typeReference.type === "container" && typeReference.container.type === "literal") {
const literal = typeReference.container.literal;
switch (literal.type) {
Expand Down
11 changes: 2 additions & 9 deletions generators/csharp/model/src/object/ObjectGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class ObjectGenerator extends FileGenerator<CSharpFile, ModelCustomConfig
];
flattenedProperties.forEach((property) => {
const fieldType = this.context.csharpTypeMapper.convert({ reference: property.valueType });
const maybeLiteralInitializer = this.context.getLiteralFromTypeReference({
const maybeLiteralInitializer = this.context.getLiteralInitializerFromTypeReference({
typeReference: property.valueType
});

Expand All @@ -55,14 +55,7 @@ export class ObjectGenerator extends FileGenerator<CSharpFile, ModelCustomConfig
summary: property.docs,
jsonPropertyName: property.name.wireValue,
useRequired: true,
initializer:
maybeLiteralInitializer != null
? csharp.codeblock(
typeof maybeLiteralInitializer === "boolean"
? `${maybeLiteralInitializer.toString().toLowerCase()}`
: `"${maybeLiteralInitializer}"`
)
: undefined
initializer: maybeLiteralInitializer
})
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CSharpFile, FileGenerator, csharp } from "@fern-api/csharp-codegen";
import { ExampleGenerator, getUndiscriminatedUnionSerializerAnnotation } from "@fern-api/fern-csharp-model";
import { ExampleGenerator } from "@fern-api/fern-csharp-model";
import { RelativeFilePath, join } from "@fern-api/fs-utils";

import {
Expand Down Expand Up @@ -113,6 +113,9 @@ export class WrappedRequestGenerator extends FileGenerator<CSharpFile, SdkCustom
inlinedRequestBody: (request) => {
for (const property of [...request.properties, ...(request.extendedProperties ?? [])]) {
const propertyName = property.name.name.pascalCase.safeName;
const maybeLiteralInitializer = this.context.getLiteralInitializerFromTypeReference({
typeReference: property.valueType
});
class_.addField(
csharp.field({
name: propertyName,
Expand All @@ -122,7 +125,8 @@ export class WrappedRequestGenerator extends FileGenerator<CSharpFile, SdkCustom
set: true,
summary: property.docs,
jsonPropertyName: addJsonAnnotations ? property.name.wireValue : undefined,
useRequired: true
useRequired: true,
initializer: maybeLiteralInitializer
})
);

Expand Down
8 changes: 8 additions & 0 deletions generators/csharp/sdk/versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
# Set `enable-forward-compatible-enums` to `false` in the configuration to generate the old enums.
# irVersion: 53

- version: 1.9.19
createdAt: "2025-02-06"
irVersion: 53
changelogEntry:
- type: fix
summary: |
Support literals in wrapped requests (). For example, if a field in a wrapped request has a literal value of "foo", we will now set that value as the default value for the field.
Note that wrapped requests are just a bag of properties that include query parameters, body properties, and headers.
- version: 1.9.19
createdAt: "2025-02-06"
changelogEntry:
Expand Down

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

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

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit 484302f

Please sign in to comment.