Skip to content

fix(spec-selector): isMediaTypeSchemaPropertiesEqual should handle case where literal media-types are equal. #6820

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

Merged
merged 3 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/core/plugins/spec/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,9 @@ export const getOAS3RequiredRequestBodyContentType = (state, pathMethod) => {
}

export const isMediaTypeSchemaPropertiesEqual = ( state, pathMethod, currentMediaType, targetMediaType) => {
if((currentMediaType || targetMediaType) && currentMediaType === targetMediaType ) {
return true
}
let requestBodyContent = state.getIn(["resolvedSubtrees", "paths", ...pathMethod, "requestBody", "content"], fromJS([]))
if (requestBodyContent.size < 2 || !currentMediaType || !targetMediaType) {
// nothing to compare
Expand Down
167 changes: 166 additions & 1 deletion test/unit/core/plugins/spec/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
parameterWithMetaByIdentity,
parameterInclusionSettingFor,
consumesOptionsFor,
taggedOperations
taggedOperations,
isMediaTypeSchemaPropertiesEqual
} from "corePlugins/spec/selectors"

import Petstore from "./assets/petstore.json"
Expand Down Expand Up @@ -1211,3 +1212,167 @@ describe("taggedOperations", function () {
})
})
})
describe("isMediaTypeSchemaPropertiesEqual", () => {
const stateSingleMediaType = fromJS({
resolvedSubtrees: {
paths: {
"/test": {
post: {
requestBody: {
content: {
"application/json": {
schema: {
properties: {
"test": "some"
}
}
}
}
}
}
}
}
}
})
const pathMethod = ["/test", "post"]

describe("Only one media type defined", () => {
const state = stateSingleMediaType

it("should return false if currentMediaType is null", () => {
const currentMediaType = null
const targetMediaType = "application/json"

const result = isMediaTypeSchemaPropertiesEqual(
state,
pathMethod,
currentMediaType,
targetMediaType
)

expect(result).toEqual(false)
})
it("should return false if currentMediaType is undefined", () => {
const currentMediaType = undefined
const targetMediaType = "application/json"

const result = isMediaTypeSchemaPropertiesEqual(
state,
pathMethod,
currentMediaType,
targetMediaType
)

expect(result).toEqual(false)
})
it("should return false if targetMediaType is null", () => {
const currentMediaType = "application/json"
const targetMediaType = null

const result = isMediaTypeSchemaPropertiesEqual(
state,
pathMethod,
currentMediaType,
targetMediaType
)

expect(result).toEqual(false)
})
it("should return false if targetMediaType is undefined", () => {
const currentMediaType = "application/json"
const targetMediaType = undefined

const result = isMediaTypeSchemaPropertiesEqual(
state,
pathMethod,
currentMediaType,
targetMediaType
)

expect(result).toEqual(false)
})
it("should return true when currentMediaType and targetMediaType are the same", () => {
const currentMediaType = "application/json"
const targetMediaType = "application/json"

const result = isMediaTypeSchemaPropertiesEqual(
state,
pathMethod,
currentMediaType,
targetMediaType
)

expect(result).toEqual(true)
})

it("should return false if currentMediaType is not targetMediaType, but only one media type defined", () => {
const currentMediaType = "application/json"
const targetMediaType = "application/xml"

const result = isMediaTypeSchemaPropertiesEqual(
state,
pathMethod,
currentMediaType,
targetMediaType
)

expect(result).toEqual(false)
})
})
describe("Multiple media types defined", () => {
const keyPath = ["resolvedSubtrees", "paths", ...pathMethod, "requestBody", "content"]
const state = stateSingleMediaType
.setIn(
[...keyPath, "application/xml"],
stateSingleMediaType.getIn([...keyPath, "application/json"])
)
.setIn(
[...keyPath, "application/other"],
stateSingleMediaType
.getIn([...keyPath, "application/json"])
.setIn(["schema", "properties"], "someOther")
)

it("should return true if same media type", () => {
const currentMediaType = "application/json"
const targetMediaType = "application/json"

const result = isMediaTypeSchemaPropertiesEqual(
state,
pathMethod,
currentMediaType,
targetMediaType
)

expect(result).toEqual(true)
})

it("should return true if target has same properties", () => {
const currentMediaType = "application/json"
const targetMediaType = "application/xml"

const result = isMediaTypeSchemaPropertiesEqual(
state,
pathMethod,
currentMediaType,
targetMediaType
)

expect(result).toEqual(true)
})

it("should return false if target has other properties", () => {
const currentMediaType = "application/json"
const targetMediaType = "application/other"

const result = isMediaTypeSchemaPropertiesEqual(
state,
pathMethod,
currentMediaType,
targetMediaType
)

expect(result).toEqual(false)
})
})
})