Skip to content

[Bug]: Missing Namespace Imports in C# Code Generated from MergePatchUpdate<T> through http-server-csharp #8374

@k117117117

Description

@k117117117

Describe the bug

This problem seems to be happened when model T(in this case Product) consists only of enum and built-in types.
I'm not sure this happens in other cases.

versions
@typespec/compiler version 1.3.0
@typespec/http version 1.3.0
@typespec/http-server-csharp version 0.58.0-alpha.19

main.tsp

import "@typespec/http";

using Http;
@service(#{ title: "Product Service" })
namespace DemoService;

model Product {
  id: uint32;
  name: string;
  category: ProductCategory;
}

enum ProductCategory {
  Electronics,
  Books,
  Clothing,
  Home
}

@route("/products")
@tag("Products")
interface Products {
  /** List products */
  @get list(): Product[];

  /** Read products */
  @get read(@path id: string): Product;
  
  /** Create a product */
  @post create(@body body: Product): Product;
  
  /** Update a product */
  @patch update(@path id: string, @body body: MergePatchUpdate<Product>): Product;
  
  /** Delete a product */
  @delete delete(@path id: string): void;
}

generated ProductMergePatchUpdate.cs

// Generated by @typespec/http-server-csharp
// <auto-generated />

using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using TypeSpec.Helpers.JsonConverters;
using TypeSpec.Helpers;

namespace TypeSpec.Http
{

    public partial class ProductMergePatchUpdate
    {
        public UInt32? Id { get; set; }

        public string Name { get; set; }

        [JsonConverter(typeof(JsonStringEnumConverter))]
        public ProductCategory? Category { get; set; } // ProductCategory NOT BE RESOLVED WITHOUT A NAMESPACE IMPORT (using DemoService;)


    }
}

ProductMergePatchUpdate.cs should be like this.

// Generated by @typespec/http-server-csharp
// <auto-generated />

using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using TypeSpec.Helpers.JsonConverters;
using TypeSpec.Helpers;
using DemoService; // THIS SHOULD BE HERE AND THIS CODE WILL BE COMPILED SUCCESSFULY

namespace TypeSpec.Http
{

    public partial class ProductMergePatchUpdate
    {
        public UInt32? Id { get; set; }

        public string Name { get; set; }

        [JsonConverter(typeof(JsonStringEnumConverter))]
        public ProductCategory? Category { get; set; }


    }
}

Reproduction

main.tsp

import "@typespec/http";

using Http;
@service(#{ title: "Product Service" })
namespace DemoService;

model Product {
  id: uint32;
  name: string;
  category: ProductCategory;
}

enum ProductCategory {
  Electronics,
  Books,
  Clothing,
  Home
}

@route("/products")
@tag("Products")
interface Products {
  /** List products */
  @get list(): Product[];

  /** Read products */
  @get read(@path id: string): Product;
  
  /** Create a product */
  @post create(@body body: Product): Product;
  
  /** Update a product */
  @patch update(@path id: string, @body body: MergePatchUpdate<Product>): Product;
  
  /** Delete a product */
  @delete delete(@path id: string): void;
}

generated ProductMergePatchUpdate.cs

// Generated by @typespec/http-server-csharp
// <auto-generated />

using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using TypeSpec.Helpers.JsonConverters;
using TypeSpec.Helpers;

namespace TypeSpec.Http
{

    public partial class ProductMergePatchUpdate
    {
        public UInt32? Id { get; set; }

        public string Name { get; set; }

        [JsonConverter(typeof(JsonStringEnumConverter))]
        public ProductCategory? Category { get; set; }


    }
}

Checklist

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions