* sm.isEmpty

* Fix error

* first test

* changelog, yasway

* bug

* commented code
This commit is contained in:
Sergey Shandar 2018-08-22 14:32:01 -07:00 коммит произвёл GitHub
Родитель 8a0ffce1cc
Коммит 299cc866be
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 133 добавлений и 81 удалений

Просмотреть файл

@ -1,5 +1,9 @@
# Changelog
### 08/22/2018 0.5.5
- CloudError bug fix #300.
### 08/22/2018 0.5.4
- Simplified source-map

Просмотреть файл

@ -15,6 +15,7 @@ import { MutableStringMap, entries } from "@ts-common/string-map"
import { SwaggerObject, ParameterObject, SchemaObject, DataType } from "yasway"
import * as jsonParser from "@ts-common/json-parser"
import { cloneDeep, Data } from "@ts-common/source-map"
import { generatedPrefix } from "../validators/resolveNestedDefinitions"
export type DocCache = MutableStringMap<Promise<SwaggerObject>>
@ -610,11 +611,7 @@ export function isPureObject(model: SchemaObject): boolean {
model.properties.length === 0
) {
return true
} else if (
!model.type &&
model.properties &&
model.properties.length === 0
) {
} else if (!model.type && model.properties && model.properties.length === 0) {
return true
} else if (
model.type &&
@ -676,12 +673,9 @@ export function relaxModelLikeEntities(model: SchemaObject): SchemaObject {
const modelProperties = model.properties
for (const [propName, property] of entries(modelProperties)) {
modelProperties[propName] = property.properties ?
relaxModelLikeEntities(property) :
relaxEntityType(
property,
isPropertyRequired(propName, model)
)
modelProperties[propName] = property.properties
? relaxModelLikeEntities(property)
: relaxEntityType(property, isPropertyRequired(propName, model))
}
}
return model
@ -782,8 +776,7 @@ export function allowNullableTypes(model: SchemaObject): SchemaObject {
for (const [propName, prop] of entries(modelProperties)) {
// process properties if present
modelProperties[propName] =
prop.properties ||
prop.additionalProperties
prop.properties || prop.additionalProperties
? allowNullableTypes(prop)
: allowNullType(prop, isPropertyRequired(propName, model))
}
@ -870,26 +863,30 @@ export const statusCodeStringToStatusCode = lodash.invert(
)
)
export const generatedCloudErrorName = generatedPrefix + "CloudError"
export const generatedCloudErrorSchemaName = generatedPrefix + "CloudErrorSchema"
export const generatedCloudErrorWrapperName = generatedPrefix + "CloudErrorWrapper"
/**
* Models an ARM cloud error schema.
*/
export const CloudErrorSchema = {
export const GeneratedCloudErrorSchema = {
description: "Error response describing why the operation failed.",
title: "#/definitions/CloudErrorSchema",
title: "#/definitions/" + generatedCloudErrorSchemaName,
schema: {
$ref: "#/definitions/CloudErrorWrapper"
$ref: "#/definitions/" + generatedCloudErrorWrapperName
}
}
/**
* Models an ARM cloud error wrapper.
*/
export const CloudErrorWrapper: SchemaObject = {
export const GeneratedCloudErrorWrapper: SchemaObject = {
type: "object",
title: "#/definitions/CloudErrorWrapper",
title: "#/definitions/" + generatedCloudErrorWrapperName,
properties: {
error: {
$ref: "#/definitions/CloudError"
$ref: "#/definitions/" + generatedCloudErrorName
}
},
additionalProperties: false
@ -898,9 +895,9 @@ export const CloudErrorWrapper: SchemaObject = {
/**
* Models a Cloud Error
*/
export const CloudError: SchemaObject = {
export const GeneratedCloudError: SchemaObject = {
type: "object",
title: "#/definitions/CloudError",
title: "#/definitions/" + generatedCloudErrorName,
properties: {
code: {
type: "string",

Просмотреть файл

@ -14,7 +14,8 @@ import * as utils from "../util/utils"
import { CommonError } from "../util/commonError"
import { ErrorCodes } from "../util/constants"
import { log } from "../util/logging"
import { StringMap, MutableStringMap, entries, keys } from "@ts-common/string-map"
import { StringMap, MutableStringMap, entries, keys, toStringMap } from "@ts-common/string-map"
import * as sm from "@ts-common/string-map"
import { Operation } from "yasway"
import * as Sway from "yasway"
import { ResponseWrapper } from "../models/responseWrapper"
@ -77,7 +78,7 @@ export class ModelValidator extends SpecValidator<SpecValidationResult> {
if (example === undefined) {
throw new Error("example is undefined")
}
if (toArray(keys(example as StringMap<unknown>)).length === 0) {
if (sm.isEmpty(toStringMap(example))) {
delete operationResult[C.exampleInSpec]
}
}

Просмотреть файл

@ -25,6 +25,8 @@ import { PartialFactory } from "@ts-common/property-set"
const skipUndefined = <T>(f: (v: T) => T): ((v: T|undefined) => T|undefined) =>
(v) => v === undefined ? undefined : f(v)
export const generatedPrefix = "generated."
export function resolveNestedDefinitions(spec: SwaggerObject): SwaggerObject {
const extraDefinitions: DefinitionsObject = {}
@ -51,7 +53,7 @@ export function resolveNestedDefinitions(spec: SwaggerObject): SwaggerObject {
const result = resolveSchemaObject(schemaObject)
const info = getInfo(result)
const suffix = info === undefined ? uuid.v4() : getPath(info).join(".")
const definitionName = "generated." + suffix
const definitionName = generatedPrefix + suffix
if (result !== undefined) {
extraDefinitions[definitionName] = result
}

Просмотреть файл

@ -18,7 +18,14 @@ import {
OperationObject
} from "yasway"
import { defaultIfUndefinedOrNull } from "../util/defaultIfUndefinedOrNull"
import { MutableStringMap, Entry, entries, values, keys, StringMap } from "@ts-common/string-map"
import {
MutableStringMap,
Entry,
entries,
values,
keys,
StringMap
} from "@ts-common/string-map"
import { resolveNestedDefinitions } from "./resolveNestedDefinitions"
import { getOperations } from "../util/methods"
import { transform } from "./specTransformer"
@ -287,13 +294,12 @@ export class SpecResolver {
const allRefsRemoteRelative = JsonRefs.findRefs(doc, options)
const e = entries(allRefsRemoteRelative as StringMap<RefDetails>)
const promiseFactories = toArray(map(
e,
([refName, refDetails]) => {
const promiseFactories = toArray(
map(e, ([refName, refDetails]) => {
return async () =>
await this.resolveRelativeReference(refName, refDetails, doc, docPath)
}
))
})
)
if (promiseFactories.length) {
await utils.executePromisesSequentially(promiseFactories)
}
@ -686,7 +692,11 @@ export class SpecResolver {
}
// scan every response in the operation
for (const response of values(operation.responses)) {
if (response.schema && !octetStream(produces) && response.schema.type !== "file") {
if (
response.schema &&
!octetStream(produces) &&
response.schema.type !== "file"
) {
response.schema = utils.relaxModelLikeEntities(response.schema)
}
}
@ -716,7 +726,10 @@ export class SpecResolver {
if (parameter.in && parameter.in === "body" && parameter.schema) {
parameter.schema = utils.relaxModelLikeEntities(parameter.schema)
}
parameters[paramName] = utils.relaxEntityType(parameter, parameter.required)
parameters[paramName] = utils.relaxEntityType(
parameter,
parameter.required
)
}
}
@ -726,14 +739,15 @@ export class SpecResolver {
private modelImplicitDefaultResponse(): void {
const spec = this.specInJson
const definitions = spec.definitions as DefinitionsObject
if (!definitions.CloudError) {
definitions.CloudErrorWrapper = utils.CloudErrorWrapper
definitions.CloudError = utils.CloudError
const addDefinition = (name: string, schema: SchemaObject) => {
if (definitions[name] !== undefined) { definitions[name] = schema }
}
for (const pathObj of values(spec.paths)) {
addDefinition(utils.generatedCloudErrorName, utils.GeneratedCloudError)
addDefinition(utils.generatedCloudErrorWrapperName, utils.GeneratedCloudErrorWrapper)
for (const pathObj of values(spec.paths!)) {
for (const operation of getOperations(pathObj)) {
if (operation.responses && !operation.responses.default) {
operation.responses.default = utils.CloudErrorSchema
operation.responses.default = utils.GeneratedCloudErrorSchema
}
}
}

98
package-lock.json сгенерированный
Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "oav",
"version": "0.5.4",
"version": "0.5.5",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -13,9 +13,9 @@
}
},
"@ts-common/iterator": {
"version": "0.0.35",
"resolved": "https://registry.npmjs.org/@ts-common/iterator/-/iterator-0.0.35.tgz",
"integrity": "sha512-zgmlyIm6280OnKBPOVWwUGF8vnuncWpTnbAsewg8R5OZwLkaLhKbKItGc2Z+2wQjnRG+VL43jTHw5zqHJeN1bQ==",
"version": "0.0.36",
"resolved": "https://registry.npmjs.org/@ts-common/iterator/-/iterator-0.0.36.tgz",
"integrity": "sha512-Df/hkK9w1WHODsOSMtPD84s9XWsf3lhWH3VirYsy8qRG7WatSlyymka/Lh8luok7U/Fv7m7onvZv4N/RanOyQA==",
"requires": {
"@ts-common/tuple": "0.0.0"
}
@ -27,6 +27,25 @@
"requires": {
"@ts-common/iterator": "0.0.35",
"@ts-common/string-map": "0.0.23"
},
"dependencies": {
"@ts-common/iterator": {
"version": "0.0.35",
"resolved": "https://registry.npmjs.org/@ts-common/iterator/-/iterator-0.0.35.tgz",
"integrity": "sha512-zgmlyIm6280OnKBPOVWwUGF8vnuncWpTnbAsewg8R5OZwLkaLhKbKItGc2Z+2wQjnRG+VL43jTHw5zqHJeN1bQ==",
"requires": {
"@ts-common/tuple": "0.0.0"
}
},
"@ts-common/string-map": {
"version": "0.0.23",
"resolved": "https://registry.npmjs.org/@ts-common/string-map/-/string-map-0.0.23.tgz",
"integrity": "sha512-V++e6pn/ISZaHrQOyRAc+/UCVa+dAa+0fGZ2cOx7PkDy47DFqwJqyIAOAKpZCQmREJi3r7LUTjSeq7Dm12RoAQ==",
"requires": {
"@ts-common/iterator": "0.0.35",
"@ts-common/tuple": "0.0.0"
}
}
}
},
"@ts-common/json-parser": {
@ -39,6 +58,25 @@
"@ts-common/source-map": "0.2.1",
"@ts-common/string-map": "0.0.23",
"tslib": "^1.9.3"
},
"dependencies": {
"@ts-common/iterator": {
"version": "0.0.35",
"resolved": "https://registry.npmjs.org/@ts-common/iterator/-/iterator-0.0.35.tgz",
"integrity": "sha512-zgmlyIm6280OnKBPOVWwUGF8vnuncWpTnbAsewg8R5OZwLkaLhKbKItGc2Z+2wQjnRG+VL43jTHw5zqHJeN1bQ==",
"requires": {
"@ts-common/tuple": "0.0.0"
}
},
"@ts-common/string-map": {
"version": "0.0.23",
"resolved": "https://registry.npmjs.org/@ts-common/string-map/-/string-map-0.0.23.tgz",
"integrity": "sha512-V++e6pn/ISZaHrQOyRAc+/UCVa+dAa+0fGZ2cOx7PkDy47DFqwJqyIAOAKpZCQmREJi3r7LUTjSeq7Dm12RoAQ==",
"requires": {
"@ts-common/iterator": "0.0.35",
"@ts-common/tuple": "0.0.0"
}
}
}
},
"@ts-common/property-set": {
@ -55,14 +93,33 @@
"@ts-common/json": "0.0.16",
"@ts-common/property-set": "0.0.7",
"@ts-common/string-map": "0.0.23"
},
"dependencies": {
"@ts-common/iterator": {
"version": "0.0.35",
"resolved": "https://registry.npmjs.org/@ts-common/iterator/-/iterator-0.0.35.tgz",
"integrity": "sha512-zgmlyIm6280OnKBPOVWwUGF8vnuncWpTnbAsewg8R5OZwLkaLhKbKItGc2Z+2wQjnRG+VL43jTHw5zqHJeN1bQ==",
"requires": {
"@ts-common/tuple": "0.0.0"
}
},
"@ts-common/string-map": {
"version": "0.0.23",
"resolved": "https://registry.npmjs.org/@ts-common/string-map/-/string-map-0.0.23.tgz",
"integrity": "sha512-V++e6pn/ISZaHrQOyRAc+/UCVa+dAa+0fGZ2cOx7PkDy47DFqwJqyIAOAKpZCQmREJi3r7LUTjSeq7Dm12RoAQ==",
"requires": {
"@ts-common/iterator": "0.0.35",
"@ts-common/tuple": "0.0.0"
}
}
}
},
"@ts-common/string-map": {
"version": "0.0.23",
"resolved": "https://registry.npmjs.org/@ts-common/string-map/-/string-map-0.0.23.tgz",
"integrity": "sha512-V++e6pn/ISZaHrQOyRAc+/UCVa+dAa+0fGZ2cOx7PkDy47DFqwJqyIAOAKpZCQmREJi3r7LUTjSeq7Dm12RoAQ==",
"version": "0.0.24",
"resolved": "https://registry.npmjs.org/@ts-common/string-map/-/string-map-0.0.24.tgz",
"integrity": "sha512-CX7+EbAQbsyaOj6huGccRq6X72Zv+abwjPl5NYKoMpt/FIQ3TtItAE6nfDAfb+28WsISkXNSubTGaMhjh8MAHA==",
"requires": {
"@ts-common/iterator": "0.0.35",
"@ts-common/iterator": "0.0.36",
"@ts-common/tuple": "0.0.0"
}
},
@ -6133,11 +6190,11 @@
}
},
"yasway": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/yasway/-/yasway-1.0.6.tgz",
"integrity": "sha512-PNB9tzgwp6Q0kz2OcifP/d6IR9EIV/p6HYRApsA+LCte3vZe8N5oCeyM3FH46SlBM9lFzGHvA9EFgHlz/ft2yg==",
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/yasway/-/yasway-1.0.7.tgz",
"integrity": "sha512-g/+RpedI+Aro75xL+L36ohpsC9eQMjgkBq9NMbKI/a/KpZD8iGOfqE3J1mW0BQva1VPfxQ7yJPrCvP0eP6zh7A==",
"requires": {
"@ts-common/string-map": "0.0.17",
"@ts-common/string-map": "0.0.24",
"debug": "^3.1.0",
"faker": "^4.1.0",
"js-base64": "^2.4.8",
@ -6153,23 +6210,6 @@
"z-schema": "^3.23.0"
},
"dependencies": {
"@ts-common/iterator": {
"version": "0.0.33",
"resolved": "https://registry.npmjs.org/@ts-common/iterator/-/iterator-0.0.33.tgz",
"integrity": "sha512-lw50Y/3j/4at879d/md0w9fWhqoucXyHi6bfNts+v3xNUczJomQ8Mu3LHyoEdIn/aUxT4Mh7WY+UpAq4MaH3zg==",
"requires": {
"@ts-common/tuple": "0.0.0"
}
},
"@ts-common/string-map": {
"version": "0.0.17",
"resolved": "https://registry.npmjs.org/@ts-common/string-map/-/string-map-0.0.17.tgz",
"integrity": "sha512-flB+JaE4ACBdFlIvUdB2ezLZ+y887Zya8AyIfjcRgqVHJ6lbfEVzIfrSSpEgHCpF0+AVqspGGKgehXRfC2WzJA==",
"requires": {
"@ts-common/iterator": "0.0.33",
"@ts-common/tuple": "0.0.0"
}
},
"commander": {
"version": "2.11.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz",

Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "oav",
"version": "0.5.4",
"version": "0.5.5",
"author": {
"name": "Microsoft Corporation",
"email": "azsdkteam@microsoft.com",
@ -10,12 +10,12 @@
"license": "MIT",
"dependencies": {
"@microsoft.azure/autorest-extension-base": "1.0.13",
"@ts-common/iterator": "0.0.35",
"@ts-common/iterator": "0.0.36",
"@ts-common/json": "0.0.16",
"@ts-common/json-parser": "0.2.0",
"@ts-common/property-set": "0.0.7",
"@ts-common/source-map": "0.2.1",
"@ts-common/string-map": "0.0.23",
"@ts-common/string-map": "0.0.24",
"@ts-common/tuple": "0.0.0",
"@types/lodash": "^4.14.116",
"@types/request": "^2.47.1",
@ -38,7 +38,7 @@
"vscode-jsonrpc": "^3.6.2",
"winston": "^3.0.0",
"yargs": "^6.6.0",
"yasway": "^1.0.6",
"yasway": "^1.0.7",
"yuml2svg": "^3.1.0"
},
"devDependencies": {

Просмотреть файл

@ -395,13 +395,6 @@ describe("Live Validator", () => {
swaggerPathsPattern: "**/*.json",
shouldModelImplicitDefaultResponse: true
}
/*
const apiUrl =
"https://management.azure.com/" +
"subscriptions/subscriptionId/providers/Microsoft.Test/storageAccounts" +
"?api-version=2016-01-01"
*/
const validator = new LiveValidator(options)
await validator.initialize()
const microsoftTest = validator.cache["microsoft.test"]
@ -422,10 +415,11 @@ describe("Live Validator", () => {
if (responses.default.schema.type === "file") {
throw new Error("responses.default.schema.type === \"file\"")
}
if (responses.default.schema.properties === undefined) {
throw new Error("responses.default.schema.properties === undefined")
}
assert.deepEqual(responses.default.schema.properties.error, utils.CloudError)
assert.deepEqual(responses.default, utils.GeneratedCloudErrorSchema)
assert.strictEqual(
responses.default.schema.$ref,
"#/definitions/" + utils.generatedCloudErrorWrapperName
)
}
})
})