Skip to content

Commit

Permalink
fix rule index & support additionalProperties:true (#263)
Browse files Browse the repository at this point in the history
* fix rule index

* fix additional properties boolean

* add removed test

* fix test

* fix test

* fix test

Co-authored-by: jianyexi <[email protected]>
  • Loading branch information
jianyexi and jianyexi authored Jan 10, 2023
1 parent 000f1f7 commit 00652a8
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 4 deletions.
8 changes: 7 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,10 @@

[1045 - AddedOptionalProperty](rules/1045.md)

[1046 - RemovedOptionalParameter](rules/1046.md)
[1046 - RemovedOptionalParameter](rules/1046.md)

[1047 - XmsEnumChanged](rules/1047.md)

[1048 - AddedXmsEnum](rules/1048.md)

[1049 - RemovedXmsEnum](rules/1049.md)
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure/oad",
"version": "0.10.3",
"version": "0.10.4",
"author": {
"name": "Microsoft Corporation",
"email": "[email protected]",
Expand Down
57 changes: 57 additions & 0 deletions src/lib/util/resolveSwagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class ResolveSwagger {
this.unifyXMsPaths()
this.ConvertPathLevelParameter()
this.ExpandDefinitions()
this.ConvertAdditionalProperty()
this.generateNew()
return this.innerSwagger
}
Expand Down Expand Up @@ -145,6 +146,62 @@ export class ResolveSwagger {
}
}
}

public ConvertAdditionalProperty() {
if (!this.innerSwagger) {
throw new Error("Null swagger object")
}
const swagger = this.innerSwagger as any
const paths = swagger.paths
if (paths && paths instanceof Object && toArray(sm.keys(paths)).length > 0) {
for (const v of sm.values(paths)) {
for (const [key, o] of sm.entries(v as any)) {
if (key.toLowerCase() !== "parameters") {
const operationParam = (o as any).parameters ? (o as any).parameters : []
operationParam.forEach((v: any) => v.schema && this.transformAdditionalProperty(v.schema))
const responses = (o as any).responses ? sm.values((o as any).responses) : []
responses.forEach((v: any) => v.schema && this.transformAdditionalProperty(v.schema))
} else {
sm.values(o as any).forEach((v: any) => v.schema && this.transformAdditionalProperty(v.schema))
}
}
}
}
if (swagger.definitions) {
for (const o of sm.values(swagger.definitions)) {
this.transformAdditionalProperty(o)
}
}
if (swagger.parameters) {
for (const o of sm.values(swagger.parameters)) {
if ((o as any).schema) {
this.transformAdditionalProperty((o as any).schema)
}
}
}
}

private transformAdditionalProperty(schema: any) {
if (typeof schema?.additionalProperties === "boolean") {
if (!schema?.additionalProperties) {
delete schema.additionalProperties
} else {
schema.additionalProperties = {}
}
}
if (schema.properties) {
for (const v of sm.values(schema.properties)) {
this.transformAdditionalProperty(v)
}
}

if (schema.allOf) {
for (const v of sm.values(schema.allOf)) {
this.transformAdditionalProperty(v)
}
}
}

private ExpandDefinitions() {
if (!this.innerSwagger) {
throw new Error("Null swagger object")
Expand Down
67 changes: 67 additions & 0 deletions src/test/additional-properties/new.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"swagger": "2.0",
"info": {
"title": "common_parameter_check_04",
"version": "1.0"
},
"parameters": {
"P0": {
"name": "p0",
"in": "query",
"type": "string"
},
"P2": {
"name": "p2",
"in": "body",
"schema": {
"type": "object",
"additionalProperties": true
}
}
},
"host": "localhost:8000",
"schemes": [ "http", "https" ],
"paths": {
"/api/Operations": {
"parameters": [
{
"$ref": "#/parameters/P0"
},
{
"$ref": "#/parameters/P2"
}
],
"get": {
"operationId": "Operations_Get",
"produces": [
"text/plain"
],
"responses": {
"default": {
"description": "Error response describing why the operation failed.",
"schema": {
"additionalProperties": true
}
},
"200": {
"description": "operation successfully .",
"schema": {
"$ref":"#/definitions/Foo"
}
}
}
}
}
},
"definitions": {
"Foo": {
"type":"object",
"properties": {
"bar": {
"type":"object",
"additionalProperties":true
}
}
}
}
}
67 changes: 67 additions & 0 deletions src/test/additional-properties/old.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"swagger": "2.0",
"info": {
"title": "common_parameter_check_04",
"version": "1.0"
},
"parameters": {
"P0": {
"name": "p0",
"in": "query",
"type": "string"
},
"P2": {
"name": "p2",
"in": "body",
"schema": {
"type": "object",
"additionalProperties": false
}
}
},
"host": "localhost:8000",
"schemes": [ "http", "https" ],
"paths": {
"/api/Operations": {
"parameters": [
{
"$ref": "#/parameters/P0"
},
{
"$ref": "#/parameters/P2"
}
],
"get": {
"operationId": "Operations_Get",
"produces": [
"text/plain"
],
"responses": {
"default": {
"description": "Error response describing why the operation failed.",
"schema": {
"additionalProperties": true
}
},
"200": {
"description": "operation successfully .",
"schema": {
"$ref":"#/definitions/Foo"
}
}
}
}
}
},
"definitions": {
"Foo": {
"type":"object",
"properties": {
"bar": {
"type":"object",
"additionalProperties":true
}
}
}
}
}
63 changes: 63 additions & 0 deletions src/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,4 +732,67 @@ describe("index", () => {
]
assert.deepStrictEqual(result, expected)
})

it("Additional Properties is boolean", async () => {
const diff = new index.OpenApiDiff({})
const oldFile = "src/test/additional-properties/old.json"
const newFile = "src/test/additional-properties/new.json"
const resultStr = await diff.compare(oldFile, newFile)
const result = JSON.parse(resultStr)
const newFilePath = fileUrl(path.resolve(newFile))
const oldFilePath = fileUrl(path.resolve(oldFile))
const expected = [
{
id: "1001",
code: "NoVersionChange",
message: "The versions have not changed.",
old: {
ref: `${oldFilePath}#`,
path: "",
location: `${oldFilePath}:1:1`
},
new: {
ref: `${newFilePath}#`,
path: "",
location: `${newFilePath}:1:1`
},
type: "Info",
docUrl: "https://github.com/Azure/openapi-diff/tree/master/docs/rules/1001.md",
mode: "Update"
},
{
code: "AddedAdditionalProperties",
docUrl: "https://github.com/Azure/openapi-diff/tree/master/docs/rules/1021.md",
id: "1021",
message: "The new version adds an 'additionalProperties' element.",
mode: "Addition",
new: {
location: ``,
path: "paths./api/Operations.get.parameters",
ref: ``
},
old: {},
type: "Error"
},
{
code: "AddedAdditionalProperties",
docUrl: "https://github.com/Azure/openapi-diff/tree/master/docs/rules/1021.md",
id: "1021",
message: "The new version adds an 'additionalProperties' element.",
mode: "Addition",
new: {
location: `${newFilePath}:16:7`,
path: "parameters.P2.schema",
ref: `${newFilePath}#/parameters/P2/schema`
},
old: {
location: `${oldFilePath}:16:7`,
path: "parameters.P2.schema",
ref: `${oldFilePath}#/parameters/P2/schema`
},
type: "Error"
}
]
assert.deepStrictEqual(result, expected)
})
})

0 comments on commit 00652a8

Please sign in to comment.