Skip to content

JSON Patch: Support real extension scenarios without copying heaps of code #2743

Closed
@HappyNomad

Description

@HappyNomad

Microsoft.AspNetCore.JsonPatch is an important library in my projects. To be useful, however, I must tweak its ApplyTo behavior as described at #2432. Judging from others' issues I've read, what I'm doing isn't unusual. It seems to be a common need. I'm thankful that at least some extension point exists, but the current extensibility mechanism is cumbersome. The API should be improved.

I slightly diverge from the spec in my scenario, but the extensibility improvement would provide benefit beyond my use case.. One may need to patch objects of types not specifically supported by the library. While the patch document adheres to the spec, one may need to apply it to object types that the library couldn't anticipate. Those target object types could belong to a proprietary database engine, for example.

IObjectAdapter was created to facilitate customization from outside the library. This intent is apparent since (1) it's in the public API, and (2) it's not used internally within the library. Judging by the large amount of code duplication necessary to use it, however, it seems it wasn't tested with a real-world customization like #2432. To customize a few lines of code, I had to copy nine files. (They were ClosedGenericMatcher.cs, ConversionResultProvider.cs, ErrorReporter.cs, IAdapter.cs, ListAdapter.cs, ObjectAdapter.cs, ObjectVisitor.cs, PocoAdapter.cs, and Resources.Designer.cs.) This duplication poses the danger that (1) the copies become out-of-date with their original versions, and (2) the customized patch behavior overly diverges from the JSON Patch spec.

Rather than let the existing API stagnate and this problem fester, I propose the following. Solicit additional real-world extension scenarios from the community. Then take all scenarios into consideration and replace or augment the IObjectAdapter extension point with an API that allows us to cleanly extend JsonPatchDocument.

As for what specific API change to make, I offer the following initial suggestion. Replace the IObjectAdapter extension point with a public interface IAdapterFactory. It defines a single method with the signature IAdapter Create( object target, JsonContract jsonContract ). Use IAdapterFactory in ObjectVisitor's private SelectAdapater method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions