Skip to content

Proposal - add support for sourcing an update value from another node of the document #146

@baywet

Description

@baywet

Hey everyone,
Thank you for putting this together, overlays are extremely helpful for cleaning up OAI descriptions owned by third parties.

One scenario we can across multiple times is the use of oneOf to specify nullability with something like this:

components:
  schema:
    Foo:
      oneOf:
        - type: string #using string here for simplicity, but you could imagine a complex object type being described instead
        - nullable: true

In our scenario, we're importing OAI descriptions to mechanically process them down the road (doc generation, code generation, etc...). When the tool downstream sees that pattern it "jumps to conclusions" and emits a union type. Although that's technically correct, it's incorrect in principle. We could of course change that tool and add a bunch of specialization conditions to say "sure, but if you see that pattern, it's not really a union type".

To clean it up before processing, we end up with an overlay that looks something like this

{
  "actions": [
    {
      "update": {"type": ["string", "null"]},
      "target": "$.components.schemas['Foo']"
    },
    {
     "target": "$.components.schemas['Foo'].oneOf",
     "remove": true
    }

Although that works, it leads to a lot of duplication we have to maintain (again, imagine that instead of type string, you have a complex object definition with a bunch of properties, etc...)

We'd much rather have the following overlay instead.

{
  "actions": [
    {
      "update-from-source": "$.components.schemas['Foo'].oneOf.[? (@.type == 'string')]",
      "target": "$.components.schemas['Foo']"
    },
    {
     "target": "$.components.schemas['Foo'].oneOf",
     "remove": true
    },
    {
     "target": "$.components.schemas['Foo']",
     "update": {"type": ["string", "null"]}
    }

A couple of notes on that example:

  • yes, it's technically more actions, but now the only thing I have in the update, is the type field, no duplication for the rest of the schema.
  • I gave an ugly name to that new "update-from-source" new action kind so we can give it a better name together.
  • This new action would effectively behave like update behaves, but source the update value to merge into the target using a JSON Path instead of a "hardcoded" value.
  • I think we'll need to define the behaviour as "source the update value first, and entirely, then apply the update" to prevent infinite recursion kind of problems.

If this new thing is desirable, I'm happy to draft something up, and potentially have it released as part of a 1.1 version of the spec.

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