get-parameter-schema.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @prettier
  3. */
  4. import { fromJS } from "immutable"
  5. import getParameterSchema from "../../../../src/helpers/get-parameter-schema"
  6. describe("getParameterSchema", () => {
  7. it("should return an empty Map when given no parameters", () => {
  8. const result = getParameterSchema()
  9. expect(result.schema.toJS()).toEqual({})
  10. expect(result.parameterContentMediaType).toEqual(null)
  11. })
  12. it("should return an empty Map when given an empty Map", () => {
  13. const result = getParameterSchema(fromJS({}))
  14. expect(result.schema.toJS()).toEqual({})
  15. expect(result.parameterContentMediaType).toEqual(null)
  16. })
  17. it("should return a schema for a Swagger 2.0 query parameter", () => {
  18. const result = getParameterSchema(
  19. fromJS({
  20. name: "id",
  21. in: "query",
  22. description: "ID of the object to fetch",
  23. required: false,
  24. type: "array",
  25. items: {
  26. type: "string",
  27. },
  28. collectionFormat: "multi",
  29. })
  30. )
  31. expect(result.schema.toJS()).toEqual({
  32. type: "array",
  33. items: {
  34. type: "string",
  35. },
  36. })
  37. expect(result.parameterContentMediaType).toEqual(null)
  38. })
  39. it("should return a schema for a Swagger 2.0 body parameter", () => {
  40. const result = getParameterSchema(
  41. fromJS({
  42. name: "user",
  43. in: "body",
  44. description: "user to add to the system",
  45. required: true,
  46. schema: {
  47. type: "array",
  48. items: {
  49. type: "string",
  50. },
  51. },
  52. })
  53. )
  54. expect(result.schema.toJS()).toEqual({
  55. type: "array",
  56. items: {
  57. type: "string",
  58. },
  59. })
  60. expect(result.parameterContentMediaType).toEqual(null)
  61. })
  62. it("should return a schema for an OpenAPI 3.0 query parameter", () => {
  63. const result = getParameterSchema(
  64. fromJS({
  65. name: "id",
  66. in: "query",
  67. description: "ID of the object to fetch",
  68. required: false,
  69. schema: {
  70. type: "array",
  71. items: {
  72. type: "string",
  73. },
  74. },
  75. style: "form",
  76. explode: true,
  77. }),
  78. {
  79. isOAS3: true,
  80. }
  81. )
  82. expect(result.schema.toJS()).toEqual({
  83. type: "array",
  84. items: {
  85. type: "string",
  86. },
  87. })
  88. expect(result.parameterContentMediaType).toEqual(null)
  89. })
  90. it("should return a schema for an OpenAPI 3.0 query parameter with `content`", () => {
  91. const result = getParameterSchema(
  92. fromJS({
  93. in: "query",
  94. name: "coordinates",
  95. content: {
  96. "application/json": {
  97. schema: {
  98. type: "object",
  99. required: ["lat", "long"],
  100. properties: {
  101. lat: {
  102. type: "number",
  103. },
  104. long: {
  105. type: "number",
  106. },
  107. },
  108. },
  109. "should-ignore/the-second-media-type": {
  110. type: "string",
  111. default: "this shouldn't be returned",
  112. },
  113. },
  114. },
  115. }),
  116. {
  117. isOAS3: true,
  118. }
  119. )
  120. expect(result.schema.toJS()).toEqual({
  121. type: "object",
  122. required: ["lat", "long"],
  123. properties: {
  124. lat: {
  125. type: "number",
  126. },
  127. long: {
  128. type: "number",
  129. },
  130. },
  131. })
  132. expect(result.parameterContentMediaType).toEqual(`application/json`)
  133. })
  134. })