Skip to content

Commit

Permalink
fix bug with $ref parameters (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
udamir committed Jan 31, 2024
1 parent c0d81db commit 3feac7c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This package provides utils to compute the diff between two Json based API docum
- Identify breaking changes
- Ensure API versioning consistency

## Supported API specifications (work on progress)
## Supported API specifications

- [OpenApi 3.0](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md)
- [AsyncApi 2.x](https://v2.asyncapi.com/docs/reference)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "api-smart-diff",
"version": "1.0.1",
"version": "1.0.2",
"description": "Generate the diff between two API specifications (OpenAPI, AsyncAPI, GraphApi, JsonSchema)",
"module": "dist/index.mjs",
"main": "dist/index.cjs",
Expand Down
4 changes: 2 additions & 2 deletions src/openapi/openapi3.mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export const paramMappingResolver: MappingResolver<number> = (before, after, ctx
const _after = after.map((a) => resolveRef(a, ctx.after.root))

for (let i = 0; i < _before.length; i++) {
const beforeIn = getStringValue(before[i], "in")
const beforeName = getStringValue(before[i], "name") ?? ""
const beforeIn = getStringValue(_before[i], "in")
const beforeName = getStringValue(_before[i], "name") ?? ""

const _afterIndex = _after.findIndex((a) => {
const afterIn = getStringValue(a, "in")
Expand Down
50 changes: 50 additions & 0 deletions test/openapi/parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,56 @@ describe("Test openapi 3 parameters compare", () => {
})
})

it("should map query $ref parameters correctly", () => {
const before = yaml`
paths:
/test/endpoint:
get:
parameters:
- $ref: "#/components/parameters/name"
components:
parameters:
name:
name: name
in: query
schema:
type: string
required: true
`
const after = yaml`
paths:
/test/endpoint:
get:
parameters:
- name: name
in: query
schema:
type: string
required: false
components:
parameters:
name:
name: name
in: query
schema:
type: string
required: true
`

const { diffs, merged } = compareOpenApi(before, after, { metaKey })

expect(diffs.length).toEqual(1)

expect(diffs[0]).toHaveProperty("description")
expect(diffs[0].description).not.toEqual("")
expect(diffs[0].type).not.toEqual("unclassified")


expect(merged.paths["/test/endpoint"].get.parameters[0][metaKey]).toMatchObject({
required: { action: DiffAction.remove, type: nonBreaking },
})
})

it("should not add rename diff on query parameter name change", () => {
const before = yaml`
paths:
Expand Down

0 comments on commit 3feac7c

Please sign in to comment.