Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSwag 14 swagger spec includes static properties in schemas #4681

Open
El-Gor-do opened this issue Jan 4, 2024 · 8 comments
Open

NSwag 14 swagger spec includes static properties in schemas #4681

El-Gor-do opened this issue Jan 4, 2024 · 8 comments

Comments

@El-Gor-do
Copy link

El-Gor-do commented Jan 4, 2024

If I have a class containing a static property, such as

public class MyClass
{
    public int PublicInstanceValue { get; set; }
    public static int PublicStaticValue { get; set; }
    private static int PrivateStaticValue { get; set; }
}

In swagger.json generated by NSwag 13.20.0, the schema for MyClass only includes PublicInstanceValue which is the correct behaviour. In NSwag 14.0.0, the schema includes PublicInstanceValue, PublicStaticValue and PrivateStaticValue unless I mark the static properties with [JsonIgnore] like this

public class MyClass
{
    public int PublicInstanceValue { get; set; }
    [JsonIgnore]
    public static int PublicStaticValue { get; set; }
    [JsonIgnore]
    private static int PrivateStaticValue { get; set; }
}

Is this a bug in NSwag 14.0.0 or is there a new option that needs to be set? I would prefer not to have to annotate all of my static properties with [JsonIgnore].

@El-Gor-do El-Gor-do changed the title NSwag 14 swagger spec includes private static properties in schemas NSwag 14 swagger spec includes static properties in schemas Jan 4, 2024
@rammba
Copy link

rammba commented Jan 10, 2024

I faced this situation and ended up with ignoring internal field also.

private int TestPrivate { get; set; }
private static int TestPrivateStatic { get; set; }
[JsonIgnore]
internal int TestInternal { get; set; }
internal static int TestInternalStatic { get; set; }

@altso
Copy link
Contributor

altso commented Jan 25, 2024

I see that private instance properties are included as well in 14.0.2.

@Robulane
Copy link

This change can cause severe service outages.

E.g. if we have a code

public class SomeClass
{
public static SomeClass Instance = new SomeClass()
}

If we tried to use generated code for this class, we will end up with a StackOverflow

Apollo3zehn pushed a commit to nexus-main/nexus that referenced this issue Mar 2, 2024
@altso
Copy link
Contributor

altso commented Apr 3, 2024

@RicoSuter any word on this issue? I am generating TypeScript classes from a 3rd party library and all of the private fields are being included in the output. I cannot apply JsonIgnore or similar attributes to the model. I am volunteering to investigate the root cause and create a pull request if needed - let me know.

@altso
Copy link
Contributor

altso commented Apr 3, 2024

I am using the following schema filter to ignore private properties:

private class NSwag4681Fix : ISchemaProcessor
{
    public void Process(SchemaProcessorContext context)
    {
        foreach (ContextualPropertyInfo property in context.ContextualType.Properties)
        {
            if (!property.PropertyInfo.GetMethod?.IsPublic ?? false)
            {
                string propertyName = context.Settings.ReflectionService.GetPropertyName(property, context.Settings);
                context.Schema.Properties.Remove(propertyName);
            }
        }
    }
}

@RicoSuter
Copy link
Owner

Not sure but latest NJS/NSwag version (soon released) might fix that.

@Basssiiie
Copy link

I had the same issue with explicitly implemented interface properties that ended up being included in the schema. Updating from v14.0.7 to v14.0.8 fixed it. Thank you for your time! 😃

@Conundraah
Copy link

I can also confirm that the issue is resolved for static properties but it seems to persist for internal properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants