-
Notifications
You must be signed in to change notification settings - Fork 9.3k
feat(oas31/style): array items label to schema name (#10794) #10795
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
adrianodpdiaz
wants to merge
8
commits into
swagger-api:master
Choose a base branch
from
adrianodpdiaz:feat/array-items-label-to-schema-name
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9f23392
feat(oas31/style): array items label schema name (#10794)
adrianodpdiaz 6f2fadd
add unit and e2e tests
adrianodpdiaz eb967ee
Merge remote-tracking branch 'upstream/master' into feat/array-items-…
adrianodpdiaz fd6f4d6
prettier fixes
adrianodpdiaz f209c16
Merge branch 'swagger-api:master' into feat/array-items-label-to-sche…
adrianodpdiaz 264f390
reuse existing getModelName
adrianodpdiaz cb4ae3a
Merge branch 'swagger-api:master' into feat/array-items-label-to-sche…
adrianodpdiaz f8d2061
Merge branch 'swagger-api:master' into feat/array-items-label-to-sche…
adrianodpdiaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/core/plugins/oas31/json-schema-2020-12-extensions/fn.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| export const decodeRefName = (uri) => { | ||
| const unescaped = uri.replace(/~1/g, "/").replace(/~0/g, "~") | ||
|
|
||
| try { | ||
| return decodeURIComponent(unescaped) | ||
| } catch { | ||
| return unescaped | ||
| } | ||
| } | ||
|
|
||
| export const getModelName = (ref) => { | ||
| if (typeof ref !== "string") return null | ||
|
|
||
| if (ref.indexOf("#/definitions/") !== -1) { | ||
| return decodeRefName(ref.replace(/^.*#\/definitions\//, "")) | ||
| } | ||
| if (ref.indexOf("#/components/schemas/") !== -1) { | ||
| return decodeRefName(ref.replace(/^.*#\/components\/schemas\//, "")) | ||
| } | ||
| return null | ||
| } |
23 changes: 23 additions & 0 deletions
23
test/e2e-cypress/e2e/features/oas31-array-items-label.cy.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| describe("OpenAPI 3.1 array items label", () => { | ||
| beforeEach(() => { | ||
| cy.visit("/?url=/documents/features/oas31-array-items-label.yaml") | ||
| cy.get(".models").click() | ||
| cy.get(".json-schema-2020-12").contains("UserList").click() | ||
| cy.get(".json-schema-2020-12-accordion").contains("users").click() | ||
| }) | ||
|
|
||
| it("should display the referenced schema name instead of 'Items' for array items", () => { | ||
| cy.get(".json-schema-2020-12-keyword--items") | ||
| .find(".json-schema-2020-12-keyword__name--primary") | ||
| .should("have.text", "User") | ||
| }) | ||
|
|
||
| it("should not display the generic 'Items' label when items is a $ref", () => { | ||
| cy.get(".json-schema-2020-12-keyword--items") | ||
| .find(".json-schema-2020-12-keyword__name--primary") | ||
| .should("not.have.text", "Items") | ||
| }) | ||
| }) |
35 changes: 35 additions & 0 deletions
35
test/e2e-cypress/static/documents/features/oas31-array-items-label.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| openapi: 3.1.0 | ||
| info: | ||
| title: Test | ||
| version: "1.0" | ||
| paths: | ||
| /users: | ||
| get: | ||
| summary: Get users | ||
| responses: | ||
| "200": | ||
| description: OK | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: "#/components/schemas/UserList" | ||
| components: | ||
| schemas: | ||
| User: | ||
| type: object | ||
| description: A user of the system. | ||
| properties: | ||
| id: | ||
| type: string | ||
| name: | ||
| type: string | ||
| UserList: | ||
| type: object | ||
| description: A list of users. | ||
| properties: | ||
| results: | ||
| type: integer | ||
| users: | ||
| type: array | ||
| items: | ||
| $ref: "#/components/schemas/User" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
test/unit/core/plugins/oas31/json-schema-2020-12-extensions/fn.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| import { makeGetTitle } from "core/plugins/oas31/json-schema-2020-12-extensions/fn" | ||
|
|
||
| const baseGetTitle = (schema) => schema?.title ?? "" | ||
|
|
||
| describe("OAS31 - json-schema-2020-12-extensions - makeGetTitle", () => { | ||
| const getTitle = makeGetTitle(baseGetTitle) | ||
|
|
||
| it("should return null when original is not a function", () => { | ||
| expect(makeGetTitle(null)).toBeNull() | ||
| expect(makeGetTitle(undefined)).toBeNull() | ||
| expect(makeGetTitle("string")).toBeNull() | ||
| }) | ||
|
|
||
| describe("when schema has a title", () => { | ||
| it("should return the title from the original getTitle", () => { | ||
| expect(getTitle({ title: "MyTitle" })).toBe("MyTitle") | ||
| }) | ||
|
|
||
| it("should prefer title over $$ref", () => { | ||
| expect( | ||
| getTitle({ | ||
| title: "MyTitle", | ||
| $$ref: "#/components/schemas/Foo", | ||
| }) | ||
| ).toBe("MyTitle") | ||
| }) | ||
| }) | ||
|
|
||
| describe("when schema has no title but has $$ref", () => { | ||
| it("should extract the schema name from a full URL $$ref", () => { | ||
| expect( | ||
| getTitle({ | ||
| $$ref: "http://localhost:3200/swagger.json#/components/schemas/Foo", | ||
| }) | ||
| ).toBe("Foo") | ||
| }) | ||
|
|
||
| it("should extract the schema name from a relative $$ref", () => { | ||
| expect(getTitle({ $$ref: "swagger.json#/components/schemas/Bar" })).toBe( | ||
| "Bar" | ||
| ) | ||
| }) | ||
|
|
||
| it("should extract the schema name from a fragment-only $$ref", () => { | ||
| expect(getTitle({ $$ref: "#/components/schemas/Foo" })).toBe("Foo") | ||
| }) | ||
| }) | ||
|
|
||
| describe("when schema has no title but has $$ref pointing to #/definitions/", () => { | ||
| it("should extract the schema name from a definitions $$ref", () => { | ||
| expect(getTitle({ $$ref: "#/definitions/Bar" })).toBe("Bar") | ||
| }) | ||
| }) | ||
|
|
||
| describe("when schema has no title and $$ref does not match known paths", () => { | ||
| it("should return empty string for an unrecognized $$ref", () => { | ||
| expect(getTitle({ $$ref: "#/other/path/Baz" })).toBe("") | ||
| }) | ||
|
|
||
| it("should return empty string for null schema", () => { | ||
| expect(getTitle(null)).toBe("") | ||
| }) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.