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

Use x-navtitle and x-slug extensions for tags. Close #50 #52

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

alexeyten
Copy link

@alexeyten alexeyten commented May 16, 2024

Use x-navtitle as a title for tag in ToC and page header.
Use x-slug as a directory name for tag and it's endpoints.

Fixes #50

Use `x-navtitle` as a title for tag in ToC and page header.
Use `x-slug` as a directory name for tag and it's endpoints.
@alexeyten alexeyten changed the title Use x-navtitle and x-slug extensions for tags (fixes #50) Use x-navtitle and x-slug extensions for tags (Close #50) May 16, 2024
@alexeyten alexeyten changed the title Use x-navtitle and x-slug extensions for tags (Close #50) Use x-navtitle and x-slug extensions for tags. Close #50 May 16, 2024
@alexeyten
Copy link
Author

@3y3 @v8tenko

Copy link
Contributor

@brotheroftux brotheroftux left a comment

Choose a reason for hiding this comment

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

Overall, I think this is a solid way to support more vendor extensions in a way that makes sense.

However, I'm a bit concerned about these changes interacting with previously defined piece of logic used to derive tag titles from endpoint -> x-navtitle, which was an array type in contrast to this PR's basic string usage of x-navtitle.

@@ -110,6 +110,15 @@ function tagsFromSpec(spec: OpenAPISpec): Map<string, Tag> {
}
}

parsed.forEach((tag: Tag) => {
if (tag['x-navtitle']) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume this is supposed to make prior x-navtitle resolution logic (defined in this file on L94) obsolete, would you say that is correct?

Copy link
Author

Choose a reason for hiding this comment

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

Yes. It overrides x-navtitle from endpoints. It was always looks very unnatural and error-prone to me. I mean, what if different endpoints uses different x-navtitle for the same tag?

But I'm trying to keep backward compatibility.

@@ -110,6 +110,15 @@ function tagsFromSpec(spec: OpenAPISpec): Map<string, Tag> {
}
}

parsed.forEach((tag: Tag) => {
if (tag['x-navtitle']) {
tag.name = tag['x-navtitle'];
Copy link
Contributor

Choose a reason for hiding this comment

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

I have my reservations about the fact that we have zero distinction between a Tag that originates from an OpenAPI spec and a Tag that gets actually used when generating the ToC. I consider having two distinct entity types a better solution, where the includer-specific entity would bear no references to spec-specific vendor extensions such as x-navtitle or x-slug. However, I do recognize that this is out of scope for this PR, since this would require a significant rewrite of these parsers/transformers.

tag.name = tag['x-navtitle'];
}
if (tag['x-slug']) {
tag.id = tag['x-slug'];
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're now modifying tag.id based on x-slug's value, I would argue this would actually break a (loose) contract on Map<string, Tag> we had prior to these changes, i.e. a Tag's name now may or may not correspond to the key under which it is stored in the map.

From my point of view, this fact makes using a map obsolete, and we should probably collect the tags in a list instead.

Copy link
Author

Choose a reason for hiding this comment

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

That's why I had to modify includer/index.ts.

But we could see this as Map<OpenAPI_tag_name, diplodoc_section>, we still need to collect all tags from endpoints in OpenAPI spec file.

Comment on lines 4 to 5
describe('tags rendering', () => {
it('renders tag and toc', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these test names poorly reflect on what's actually being tested. This PR is implementing a mechanism to override page headings and URLs when specific vendor extensions are used. I could suggest naming the test suite something along the lines of Identity of pages derived from spec tags with cases named like can be overriden using x-slug to define a custom URL. Anyway, I'm open to suggestions here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, I think it's not a bad idea to split this test case, since this looks to me like three independent test cases.

Copy link
Author

Choose a reason for hiding this comment

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

I've split test cases

@alexeyten
Copy link
Author

alexeyten commented Jun 5, 2024

However, I'm a bit concerned about these changes interacting with previously defined piece of logic used to derive tag titles from endpoint -> x-navtitle, which was an array type in contrast to this PR's basic string usage of x-navtitle.

x-navtitle from tag will override x-navtitle from endpoints.
If one don't use x-navtitle in tag then nothing changes.

paths:
  /test:
    post:
      ......
      tags:
        - tag1
        - tag2
      x-navtitle:
        - Title for tag1
        - Title for tag2
  /other:
    post:
      ......
      tags:
        - tag1
        - tag3
      x-navtitle:
        - New title for tag1 # will override title from endpoint above
        - Title for tag3

tags:
  - name: tag1
    description: Description for tag1
  - name: tag2
    description: Description for tag2
  - name: tag3
    x-navtitle: New title for tag3 # will override any title from endpoints

Will result in

tags:
  - id: tag1
    name: New title for tag1 # from paths./other.post
    description: Description for tag1
  - id: tag2
    name: Title for tag2 # from paths./test.post
    description: Description for tag2
  - id: tag3
    name: New title for tag3 # from tags[name=tag3]

@brotheroftux
Copy link
Contributor

brotheroftux commented Jun 5, 2024

x-navtitle from tag will override x-navtitle from endpoints.

True. I don't think I was clear enough in my original comment — my issue with this interaction is that after merging the proposed changes, the spec will end up with two distinct ways of using the x-navtitle custom property, both with different types and semantics.

Also, I think your example really highlights reasons why I think having x-navtitle at an endpoint level does more harm than good, and why I'm strongly in favor of just nuking this old behavior (although that would, obviously, break backwards compatibility, and I'm not sure whether we have good data on the directive usage at all).

paths:
  /test:
    post:
      ......
      tags:
        - tag1
        - tag2
      x-navtitle:
        - Title for tag1
        - Title for tag2
  /other:
    post:
      ......
      tags:
        - tag1
        - tag3
      x-navtitle:
        - New title for tag1 # will override title from endpoint above
        - Title for tag3

As I see it, this usage is a) non-descriptive and confusing, and b) introduces more potential pitfalls. Also, it just makes sense to define diplodoc section names in tags, which is centralized and closer to the root of the spec. So I would be completely down to just deprecate this option and remove it after a grace period of one major library version.

Even if we don't end up deprecating the old behavior, I would argue in favor of choosing a different property identifier for the new behavior.

Anyway, I would like to hear your thoughts on this. I would like to hear @3y3 @v8tenko 's opinion on this as well.

@3y3 3y3 force-pushed the master branch 2 times, most recently from 545f9c4 to e5eacec Compare July 11, 2024 01:43
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

Successfully merging this pull request may close these issues.

Поддержать x-navtitle и x-slug в тегах
2 participants