From 8f6879897cb9e65da48086814ccc62d47a4c929f Mon Sep 17 00:00:00 2001 From: Dan Moran Date: Fri, 18 Oct 2019 16:24:52 -0400 Subject: [PATCH] Fix rendering for default/example values of 0. --- src/core/components/parameter-row.jsx | 28 ++-- .../bugs/5573-zero-default-example-values.jsx | 145 ++++++++++++++++++ 2 files changed, 161 insertions(+), 12 deletions(-) create mode 100644 test/mocha/bugs/5573-zero-default-example-values.jsx diff --git a/src/core/components/parameter-row.jsx b/src/core/components/parameter-row.jsx index 81a62b5e646..4deaadd9b77 100644 --- a/src/core/components/parameter-row.jsx +++ b/src/core/components/parameter-row.jsx @@ -104,7 +104,7 @@ export default class ParameterRow extends Component { .get("content", Map()) .keySeq() .first() - + const generatedSampleValue = getSampleSchema(schema.toJS(), parameterMediaType, { includeWriteOnly: true }) @@ -119,17 +119,21 @@ export default class ParameterRow extends Component { //// Find an initial value if (specSelectors.isSwagger2()) { - initialValue = paramWithMeta.get("x-example") - || paramWithMeta.getIn(["schema", "example"]) - || schema.getIn(["default"]) + initialValue = [ + paramWithMeta.get("x-example"), + paramWithMeta.getIn(["schema", "example"]), + schema.getIn(["default"]) + ].find(v => v !== undefined) } else if (specSelectors.isOAS3()) { const currentExampleKey = oas3Selectors.activeExamplesMember(...pathMethod, "parameters", this.getParamKey()) - initialValue = paramWithMeta.getIn(["examples", currentExampleKey, "value"]) - || paramWithMeta.getIn(["content", parameterMediaType, "example"]) - || paramWithMeta.get("example") - || schema.get("example") - || schema.get("default") - || paramWithMeta.get("default") // ensures support for `parameterMacro` + initialValue = [ + paramWithMeta.getIn(["examples", currentExampleKey, "value"]), + paramWithMeta.getIn(["content", parameterMediaType, "example"]), + paramWithMeta.get("example"), + schema.get("example"), + schema.get("default"), + paramWithMeta.get("default") // ensures support for `parameterMacro` + ].find(v => v !== undefined) } //// Process the initial value @@ -144,8 +148,8 @@ export default class ParameterRow extends Component { if(initialValue !== undefined) { this.onChangeWrapper(initialValue) } else if( - schema.get("type") === "object" - && generatedSampleValue + schema.get("type") === "object" + && generatedSampleValue && !paramWithMeta.get("examples") ) { // Object parameters get special treatment.. if the user doesn't set any diff --git a/test/mocha/bugs/5573-zero-default-example-values.jsx b/test/mocha/bugs/5573-zero-default-example-values.jsx new file mode 100644 index 00000000000..d45b24df232 --- /dev/null +++ b/test/mocha/bugs/5573-zero-default-example-values.jsx @@ -0,0 +1,145 @@ +/* eslint-env mocha */ +import React from "react" +import { List, fromJS } from "immutable" +import expect, { createSpy } from "expect" +import { render } from "enzyme" +import ParameterRow from "components/parameter-row" + +describe("bug #5573: zero default and example values", function(){ + it("should apply a Swagger 2.0 default value of zero", function(){ + const paramValue = fromJS({ + description: "a pet", + type: "integer", + default: 0 + }) + + let props = { + getComponent: ()=> "div", + specSelectors: { + security(){}, + parameterWithMetaByIdentity(){ return paramValue }, + isOAS3(){ return false }, + isSwagger2(){ return true } + }, + fn: {}, + operation: {get: ()=>{}}, + onChange: createSpy(), + param: paramValue, + rawParam: paramValue, + onChangeConsumes: () => {}, + pathMethod: [], + getConfigs: () => { return {} }, + specPath: List([]) + } + + render() + + expect(props.onChange).toHaveBeenCalled() + expect(props.onChange).toHaveBeenCalledWith(paramValue, "0", false) + }) + it("should apply a Swagger 2.0 example value of zero", function() { + const paramValue = fromJS({ + description: "a pet", + type: "integer", + schema: { + example: 0 + } + }) + + let props = { + getComponent: ()=> "div", + specSelectors: { + security(){}, + parameterWithMetaByIdentity(){ return paramValue }, + isOAS3(){ return false }, + isSwagger2(){ return true } + }, + fn: {}, + operation: {get: ()=>{}}, + onChange: createSpy(), + param: paramValue, + rawParam: paramValue, + onChangeConsumes: () => {}, + pathMethod: [], + getConfigs: () => { return {} }, + specPath: List([]) + } + + render() + + expect(props.onChange).toHaveBeenCalled() + expect(props.onChange).toHaveBeenCalledWith(paramValue, "0", false) + }) + it("should apply an OpenAPI 3.0 default value of zero", function(){ + const paramValue = fromJS({ + description: "a pet", + schema: { + type: "integer", + default: 0 + } + }) + + let props = { + getComponent: ()=> "div", + specSelectors: { + security(){}, + parameterWithMetaByIdentity(){ return paramValue }, + isOAS3(){ return true }, + isSwagger2() { return false } + }, + oas3Selectors: { + activeExamplesMember: () => null + }, + fn: {}, + operation: {get: ()=>{}}, + onChange: createSpy(), + param: paramValue, + rawParam: paramValue, + onChangeConsumes: () => {}, + pathMethod: [], + getConfigs: () => { return {} }, + specPath: List([]) + } + + render() + + expect(props.onChange).toHaveBeenCalled() + expect(props.onChange).toHaveBeenCalledWith(paramValue, "0", false) + }) + it("should apply an OpenAPI 3.0 example value of zero", function() { + const paramValue = fromJS({ + description: "a pet", + schema: { + type: "integer", + example: 0 + } + }) + + let props = { + getComponent: ()=> "div", + specSelectors: { + security(){}, + parameterWithMetaByIdentity(){ return paramValue }, + isOAS3(){ return true }, + isSwagger2() { return false } + }, + oas3Selectors: { + activeExamplesMember: () => null + }, + fn: {}, + operation: {get: ()=>{}}, + onChange: createSpy(), + param: paramValue, + rawParam: paramValue, + onChangeConsumes: () => {}, + pathMethod: [], + getConfigs: () => { return {} }, + specPath: List([]) + } + + render() + + expect(props.onChange).toHaveBeenCalled() + expect(props.onChange).toHaveBeenCalledWith(paramValue, "0", false) + }) +})