Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions packages/http-server-csharp/src/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ import {
isEmptyResponseModel,
isRecord,
isStringEnumType,
isValueType,
resolveReferenceFromScopes,
} from "./utils.js";

Expand Down Expand Up @@ -400,6 +399,10 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
const exceptionConstructor = isErrorType
? this.getModelExceptionConstructor(this.emitter.getProgram(), model, name, className)
: "";
// Add System.Diagnostics.CodeAnalysis namespace for [SetsRequiredMembers] attribute used in exception constructors
if (isErrorType && exceptionConstructor) {
checkOrAddNamespaceToScope("System.Diagnostics.CodeAnalysis", this.emitter.getContext().scope);
}

this.#metadateMap.set(model, new CSharpType({ name: className, namespace: namespace }));
const decl = this.emitter.result.declaration(
Expand All @@ -425,8 +428,9 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
if (!isErrorModel(program, model)) return undefined;
const constructor = this.getExceptionConstructorData(program, model, modelName);
const isParent = !!model.derivedModels?.length;
return `public ${className}(${constructor.properties}) : base(${constructor.statusCode?.value ?? `400`}${constructor.header ? `, \n\t\t headers: new(){${constructor.header}}` : ""}${constructor.value ? `, \n\t\t value: new{${constructor.value}}` : ""})
{ ${constructor.body ? `\n${constructor.body}` : ""}\n\t}${isParent ? `\npublic ${className}(int statusCode, object? value = null, Dictionary<string, string>? headers = default): base(statusCode, value, headers) {}\n` : ""}`;
return `[SetsRequiredMembers]
public ${className}(${constructor.properties}) : base(${constructor.statusCode?.value ?? `400`}${constructor.header ? `, \n\t\t headers: new(){${constructor.header}}` : ""}${constructor.value ? `, \n\t\t value: new{${constructor.value}}` : ""})
{ ${constructor.body ? `\n${constructor.body}` : ""}\n\t}${isParent ? `\n[SetsRequiredMembers]\npublic ${className}(int statusCode, object? value = null, Dictionary<string, string>? headers = default): base(statusCode, value, headers) {}\n` : ""}`;
}

isDuplicateExceptionName(name: string): boolean {
Expand Down Expand Up @@ -525,6 +529,7 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
const modelName = ensureCSharpIdentifier(this.emitter.getProgram(), model, name);
const modelFile = this.emitter.createSourceFile(`generated/models/${modelName}.cs`);
modelFile.meta[this.#sourceTypeKey] = CSharpSourceType.Model;
modelFile.meta["nullable"] = true;
const ns = model.namespace ?? getModelNamespace(model);
const modelNamespace = this.#getOrAddNamespace(ns);
return this.#createModelContext(modelNamespace, modelFile, modelName);
Expand All @@ -539,6 +544,7 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
);
const sourceFile = this.emitter.createSourceFile(`generated/models/${modelName}.cs`);
sourceFile.meta[this.#sourceTypeKey] = CSharpSourceType.Model;
sourceFile.meta["nullable"] = true;
const modelNamespace = this.#getOrAddNamespace(model.namespace);
const context = this.#createModelContext(modelNamespace, sourceFile, model.name);
context.instantiationName = modelName;
Expand Down Expand Up @@ -678,12 +684,10 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
property.defaultValue,
) ?? typeDefault;
const attributeList = [...attributes.values()];
const requiredModifier = !property.optional && !defaultValue ? "required " : "";
const nullableSuffix = property.optional || nullable ? "?" : "";
return this.emitter.result
.rawCode(code`${doc ? `${formatComment(doc)}\n` : ""}${`${attributeList.map((attribute) => attribute.getApplicationString(this.emitter.getContext().scope)).join("\n")}${attributeList?.length > 0 ? "\n" : ""}`}public ${this.#isInheritedProperty(property) ? "new " : ""}${typeName}${
isValueType(this.emitter.getProgram(), property.type) && (property.optional || nullable)
? "?"
: ""
} ${propertyName} { get; ${typeDefault ? "}" : "set; }"}${
.rawCode(code`${doc ? `${formatComment(doc)}\n` : ""}${`${attributeList.map((attribute) => attribute.getApplicationString(this.emitter.getContext().scope)).join("\n")}${attributeList?.length > 0 ? "\n" : ""}`}public ${this.#isInheritedProperty(property) ? "new " : ""}${requiredModifier}${typeName}${nullableSuffix} ${propertyName} { get; ${typeDefault ? "}" : "set; }"}${
defaultValue ? ` = ${defaultValue};\n` : "\n"
}
`);
Expand Down
Loading