Skip to content

ServiceStack Reference generation in C# with nested classes #805

@starafdar1

Description

@starafdar1

Describe the issue

We recently upgraded from ServiceStack version 4 to version 8.2.2. Following the upgrade, we are encountering problems generating the ServiceStack references using the x tool. The problems are related to the usage of nested classes; we have a large number of nested classes in our DTOs for various child objects. (I realize that it's not a recommended practice as it creates problems with languages that do not support nested types, but we already have quite a few of these classes and it is difficult now to change them).

There are two scenarios I have identified so far:

Scenario 1:
When a nested class is present and another class with the same name is present in the same namespace (either as a top-level class or nested under a different class), the reference generated includes only one of the classes and ignores the other classes. This causes a compile error on the client.

This seems to be due to this code in NativeTypesMetadata.cs:245 The code checks for duplicates against a hash containing the namspace and the class name. Therefore, classes are being excluded if the class name was already encountered in the namespace, even though it is at a different nesting level.

Scenario 2:
When a class is derived from a generic class with a nested class as the generic argument, the code emitted does not decorate the nested class correctly. For example, for a class as follows:

public class Response1 : QueryResult<Response1.ResponseCore1>

The output being generated is

public class Response1 : QueryResult<ResponseCore1>

This also causes a compile error in the IDE.

This may be happening in the code in CSharpGeneratorExtensions.GetInherits() method. The code seems to be not taking into account the Declaring Type.

We have a large number of classes that follow this pattern. I am hoping that these issues can be fixed since moving all our nested classes to the top level will require a significant effort on our end.

Lastly, I wanted to confirm that while nested classes are not recommended, they are still supported in the latest versions of ServiceStack. I haven't seen any errors so far when calling the web services containing these nested classes, but I wanted to verify this.

Thanks.

Reproduction

  • Create a new self-hosted app project using x new selfhost NestedClasses
  • Add the NestedClasses.cs file from this gist to the ServiceModel project Gist
  • Add the methods below to the MyService.cs file.
  • Run x csharp https://localhost:5001/ or browse to https://localhost:5001/types/csharp to view the generated ServiceStack reference classes.

Methods to add to MyService.cs

    public object Any(Parent1 request)
    {
        return null;
    }

    public object Any(ServiceModel.Parent2 request)
    {
        return null;
    }

Expected behavior

  • The generated output should include the definition of the top-level Child1 class. It is not included, only the Parent1.Child1 class definition is included.
  • The Response1 class should be defined as public class Response1 : QueryResult<Response1.ResponseCore1>. Instead, it is defined as public class Response1 : QueryResult<ResponseCore1>.

System Info

We are on .Net Framework 4.8. I was also able to reproduce this in a .Net 8 sample app.
ServiceStack version is 8.2.2.

Additional context

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions