Skip to content

v3.2: $self field (Alternative Approach) #4556

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

Open
wants to merge 12 commits into
base: v3.2-dev
Choose a base branch
from

Conversation

handrews
Copy link
Member

@handrews handrews commented Apr 29, 2025

Fixes:

This is an alternative to PR #4389. The only functional difference is that $self is required to be absolute (a change that @karenetheridge and @mikekistler are debating).

The main difference is that I drastically reduced the normative specification text and instead gave examples of all of the different ways to establish a base URI. [EDIT: and put the examples in an appendix] This is probably more understandable than trying to explain the options in prose.

@notEthan I also dumped a lot of the use cases as I think I figured out a more concise way to get the point across.

This adds $self as a way for a document to define its own URI for use in reference targets, and as the base URI for relative URI references in the document.

This does not impact the resolution of relative API URLs.

  • schema changes are included in this pull request
  • schema changes are needed for this pull request but not done yet
  • no schema changes are needed for this pull request

@handrews handrews added the re-use: ref/id resolution how $ref, operationId, or anything else is resolved label Apr 29, 2025
@handrews handrews added this to the v3.2.0 milestone Apr 29, 2025
@handrews handrews requested review from a team as code owners April 29, 2025 04:17
@handrews
Copy link
Member Author

handrews commented Apr 30, 2025

After staring at the giant examples section a lot, I decided it works better as an appendix. (force push is just because I accidentally picked up a stray file in the commit and had to edit it to pull that out).

@lornajane
Copy link
Contributor

This is the most readable of the lot, and I think I "get" the whole thing a lot more now. Thank you for doing this work and all the iterations!

Minor points/suggestions from me (I'm not ready to approve but I've only got small questions):

  • in the proposal we called it self but it looks like now it is $self. We have other URI fields with plain-text names, why does this one need the $? Since it's being added at the top level, we can be confident of not having name clashes. (why isn't it in info with the other document information like license? Am I asking too many questions?)
  • I'd like to propose we move the appendix to the learn site and link to it. Is the learn site really good/mature/stable enough to link to from a spec file? I think it is, and the information would be better there (but needs to be very well signposted since we haven't done this for other content).

@lornajane
Copy link
Contributor

Good discussion in the meeting:

  • content does need to go to the learn site, but we're not going to block this change for it
  • $self makes sense because it's like $ref in being "special" and related to how references work

@karenetheridge
Copy link
Member

I am in agreement with the shuffling of the examples to the learn site rather than in the main spec, but not with the change from relative to absolute uri.

@mikeschinkel
Copy link

@handrews — A big +1 from me for including examples in the spec. I would prefer them to be in-line so a reader does not have to jump around to follow them, but if those examples need to be in an appendix that is better than not having any or having a link to someplace that might eventually 404.

My biggest pet peeve about specs in general is that they do not include examples which make most specs all-but-inaccessible to those like me who need examples to understand the prose.

P.S. Moving to the learn site only works IMO if there is a guarantee that the learn site will never change those examples nor take them down which, I believe, is impossible to guarantee. FTW.

@ralfhandl
Copy link
Contributor

Relative vs. absolute $self doesn't seem to make much of a difference.

  • A relative $ref is now relative to the retrieval URL. Making it relative to a relative $self which is itself relative to the retrieval URL only adds the convenience of making the $ref value shorter. It does not add a feature.

  • A relative $ref that is relative to an absolute $self also only adds the convenience of making the $ref value shorter (and relative instead of absolute). It does not add a feature.

Seems the only feature added by $self is an in-document identifier for an OpenAPI description, and that feature seems to benefit from an absolute URI value.

What am I missing?

Copy link
Contributor

@mikekistler mikekistler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there may be some errors in the examples of URI resolution in Appendix G. I pointed out two and I think the rest should probably be checked for accuracy before we merge this.

src/oas.md Outdated
For the relative `$ref` in the first document, it is resolved against `$self` to produce `https://example.com/shared/foo#/components/requestBodies/Foo`.
The portion of that URI before the '#' matches the `$self` of the second document, so the reference target is resolved to `#/components/requestBodies/Foo` in that second document.

In that document, the `$ref` in the Request Body Object is resolved using that document's `$self` as the base URI, producing `https://example.com/schemas/foo`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too does not jive with the resolver that Copilot built. It says that the target URI is actually "https://example.com/api/schemas/foo".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikekistler should be fixed. Really, "these look wrong" would have been sufficient here. The errors were just because I reworked the various paths at some point to make the examles cover more cases, and some things didn't get updated post-rework. Nothing obscure was going on.

Although it sounds like you produced something useful beyond this which is cool!

@karenetheridge
Copy link
Member

Another reason to allow $self to be relative is that it is consistent with JSON Schema, where $id can be relative, so it would be easier to reason through how $ref resolution works in a document and apply RFC3986 consistently if both keywords worked the same way.

I see no gain from forcing $self to be absolute; it just restricts what users and implementations can do with the keyword. We could just as easily say that $self is not needed at all, because the user can always supply the base URI via the retrieval URI. Our goal should be to open up usecases, not restrict them.

@ralfhandl
Copy link
Contributor

ralfhandl commented May 8, 2025

$self is not needed at all, because the user can always supply the base URI via the retrieval URI

The main feature $self adds is an in-document identifier allowing to determine whether documents retrieved from different URIs or locally stored without their retrieval URI are actually the same and not just textually identical by chance.

Using $self with a relative URI means opting out its main feature and can be considered an abuse of $self. This abuse would be prevented by requiring an absolute URI value.

@ralfhandl ralfhandl requested a review from mikekistler May 8, 2025 06:27
@karenetheridge
Copy link
Member

Using $self with a relative URI means opting out its main feature and can be considered an abuse of $self

Is there an example (for the learn site, appendix or wherever) that you can construct that would demonstrate this issue, as a way of cautioning against it?

@lornajane
Copy link
Contributor

After many discussions about relative vs absolute, we agreed in TDC this week that supporting both would make a more open feature and since the retrieval URL is already used in other places, the additional burden for tools maintainers is not huge.

@handrews
Copy link
Member Author

handrews commented May 8, 2025

I have updated this with the outcome of today's TDC call. I have also:

  • @ralfhandl further clarified the consequences of using alternative URIs
  • Added an example (a random UUID URN) for the application-specific default option
  • Added an example of relative $self and $id

The new commits were added in one push, and then I force-pushed the rebased commits unchanged to pick up the latest syntax highlighting that supports this change building without warnings.

@handrews
Copy link
Member Author

handrews commented May 8, 2025

hmm... I thought I was only adding two new commits, looks like maybe I had not pushed some other local fixes? My apologies for any confusion, just be aware that I have never edited commits once pushed, so if a commit looks familiar it's the same as it was.

handrews added 6 commits May 11, 2025 08:34
This adds `$self` as a way for a document to define its own URI
for use in reference targets, and as the base URI for relative URI
references in the document.

This does not impact the resolution of relative API URLs.
Including fixing a bug in one of the URIs.
Never change your directory structure halfway through
writing examples...
Fix the $self resolution examples to consistently use /api/,
which is used in all of the relevant base URIs and is not impacted
by any of the relative paths.
handrews added 2 commits May 11, 2025 08:34
This also further clarifies the need to use `$self` in reference
targets for interoperability even if other URIs might work
in some cases.
@handrews
Copy link
Member Author

One more new bugfix commit plus an unmodified rebase force-push.

Copy link
Contributor

@ralfhandl ralfhandl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to the content, suggestions for its representation

@ralfhandl ralfhandl requested a review from a team May 12, 2025 07:48
@handrews
Copy link
Member Author

handrews commented May 12, 2025

@ralfhandl I have tried to address everything that I did not accept as a commit.

  • Everything is now "retrieval URI"
  • I reworked the requirement-then-caveat ordering
  • I reworked the crossing-$id text and dropped the mention of the unclear "point too but not across $id" case; I think @karenetheridge had wanted that included? But it's tricky to word without just being more confusing, and idk how much we should try to compensate for JSON Schema's shortcomings here
  • I added "only" to the weird JSON Pointer case that you asked if people actually do, because I agree it read strangely, and I think the new wording is much more clear

Regarding the schema media type parameter, while that usage is the most correct usage, it is a bit confusing because of the need to put YYYY-MM-DD in the URI, so I might be willing to drop it on those grounds. Otherwise, we'll need to update that to the correct date so people don't copy-paste the placeholder.

...because the YYYY-MM-DD in the URI makes it more trouble than it's worth.
@handrews
Copy link
Member Author

@ralfhandl went ahead and removed the parameter because of the YYYY-MM-DD problem. Which makes me sad because I would rather promote the long-established correct use of the media type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
re-use: ref/id resolution how $ref, operationId, or anything else is resolved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants