RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.78k stars 1.29k forks source link

Renaming properties using custom contract resolver has no effect on the swagger document #1270

Open statler opened 6 years ago

statler commented 6 years ago

Renaming properties per the blog examples does affect the swagger document. The property is properly renamed in the CreateProperty method, but the generated swagger JSON is unchanged. Do I misunderstand how this works?

    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        var className = property.DeclaringType.Name;

        if (IsRenamed(property.DeclaringType, property.PropertyName, out var newJsonPropertyName))
            property.PropertyName = newJsonPropertyName;

        return property;
    }

If I change both the PropertyName and UnderlyingName like this, it works. But just changing the UnderlyingName doesnt.

        if (IsRenamed(property.DeclaringType, property.PropertyName, out var newJsonPropertyName))
        {
            property.UnderlyingName = newJsonPropertyName;
            property.PropertyName = newJsonPropertyName;
        }

I was hoping the way it worked was that both the original (UnderlyingName) and PropertyName were stored in the JSON. In this case, the swagger doc should show the underlyingname (because there is no point accessing the wrong name for the interface), and the new name (propertyname) could be stored in the JSON so that the client generators could create new models based which expose the renamed property, but create JSON that works with the API

RicoSuter commented 6 years ago

Did you register the custom contract resolver in the generator settings?

statler commented 6 years ago

Yes. I have ignores working perfectly, and as per the original issue, it works if I set both PropertyName and UnderlyingName

RicoSuter commented 6 years ago

Strange that you also have to rename the UnderlyingName... any idea why? If this is the case, I should probably update the blog...

statler commented 6 years ago

Don't know why - not that it really matters when you think about it, because renaming an item in the contract is inherently useless if you can't also include the original name in the swagger json. In the end, I have done the renaming in the client (refer https://github.com/statler/NJsonSchema)

tmacht commented 5 years ago

Did you register the custom contract resolver in the generator settings?

How do you register a custom contract resolver in the generator settings?

RicoSuter commented 5 years ago

Register it in the mvc json options