Skip to content

Commit

Permalink
Portman - Make OpenAPI data immutable (#636)
Browse files Browse the repository at this point in the history
* Added test for OAS schema validation

* Portman - Make OpenAPI data immutable

* Portman - Make OpenAPI data immutable

* updated changelog
  • Loading branch information
thim81 authored Aug 19, 2024
1 parent cd6016a commit 561e2f5
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 43 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [Unreleased]

- Portman - Make OpenAPI data immutable (#630)

## v1.29.2 - (2024-08-19)

- Skip read-only properties from request body (#628)
Expand Down
83 changes: 83 additions & 0 deletions __tests__/fixtures/oas-oneof.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
openapi: 3.0.3
info:
title: 630 - Category API
version: 1.0.0

paths:
/categories:
get:
summary: Get a list of categories
responses:
"200":
description: A list of Category Objects
content:
application/json:
schema:
$ref: '#/components/schemas/CategoriesResponse'

components:
schemas:
CategoriesResponse:
type: object
properties:
categories:
type: array
items:
oneOf:
- $ref: '#/components/schemas/CategoryObject'
- $ref: '#/components/schemas/CategoryGroupObject'

CategoryObject:
type: object
additionalProperties: false
properties:
id:
type: integer
format: int64
description: A system defined unique identifier for the category.
example: 1
name:
type: string
minLength: 1
maxLength: 100
description: The name of the category.
example: Category Name A
is_group:
type: boolean
enum: [false]
description: For CategoryObject, is_group is always false.
example: false
required:
- id
- name
- is_group

CategoryGroupObject:
type: object
properties:
id:
type: integer
format: int64
description: A system defined unique identifier for the category.
example: 3
name:
type: string
minLength: 1
maxLength: 100
description: The name of the category.
example: Category Name A
is_group:
type: boolean
enum: [true]
description: For CategoryGroupObject, is_group is always true.
example: false
group_id:
type: integer
format: int64
nullable: true
description: The ID of the category group.
example: 1
required:
- id
- name
- is_group
31 changes: 31 additions & 0 deletions src/Portman.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,37 @@ describe('Portman', () => {
const finalCollection = JSON.parse(await fs.readFile(outputFilePath, 'utf8'))
expect(omitKeys(finalCollection, ['id', '_postman_id', 'postman_id', 'info'])).toMatchSnapshot()
}, 30000)

it('convert oneOf OAS, verify schema', async () => {
const outputFile = `./tmp/converted/crmApi.${uuidv4()}.json`

const portman = new Portman({
oaLocal: './__tests__/fixtures/oas-oneof.yaml',
portmanConfigFile: 'portman-config.default.json',
portmanConfigPath: 'portman-config.default.json',
postmanConfigFile: './__tests__/fixtures/postman-config.run.json',
postmanConfigPath: './__tests__/fixtures/postman-config.run.json',
baseUrl: 'http://localhost:3050',
output: outputFile,
syncPostman: false,
includeTests: true,
runNewman: false
})

await portman.run()

const outputFilePath = path.resolve(outputFile)
expect(await fs.pathExists(outputFilePath)).toBe(true)

const finalCollection = JSON.parse(await fs.readFile(outputFilePath, 'utf8'))
if (finalCollection?.item?.[0]?.event?.[0]?.script?.exec) {
const result = finalCollection.item[0].event[0].script.exec // Convert V1
expect(result).toMatchSnapshot()
} else {
const result = finalCollection.item[0].item[0].event[0].script.exec // Convert V2
expect(result).toMatchSnapshot()
}
}, 30000)
})

describe('Portman version', () => {
Expand Down
11 changes: 6 additions & 5 deletions src/Portman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { PortmanOptions } from './types/PortmanOptions'
import { validate } from './utils/PortmanConfig.validator'
import { PortmanError } from './utils/PortmanError'
import { changeCase } from 'openapi-format'
import _ from 'lodash'

export class Portman {
config: PortmanConfig
Expand Down Expand Up @@ -294,13 +295,13 @@ export class Portman {
async convertToPostmanCollection(): Promise<void> {
// --- openapi-to-postman - Transform OpenApi to Postman collection
const { postmanConfigPath, localPostman } = this.options

const oaToPostman = new OpenApiToPostmanService()
// TODO investigate better way to keep oasParser untouched
// Clone oasParser to prevent altering with added minItems maxItems
const { oas } = this.oasParser

// Clone oasParser to prevent altering with added minItems, maxItems and JSON schema
const oasCopy = _.cloneDeep(this.oasParser.oas)

const oaToPostmanConfig: IOpenApiToPostmanConfig = {
openApiObj: { ...oas },
openApiObj: oasCopy,
outputFile: `${process.cwd()}/tmp/working/tmpCollection.json`,
configFile: postmanConfigPath as string
}
Expand Down
Loading

0 comments on commit 561e2f5

Please sign in to comment.