Skip to content

Commit

Permalink
Correctly lookup the properties in lowercase (stoplightio#1268)
Browse files Browse the repository at this point in the history
* do not loweracse

* make createJsonSchemaFromParams pure

* lowercase on property lookup in the schema

* add harness

* changelog: add line

* auto get the correct version

* release beta

* Update CHANGELOG.md

Co-authored-by: Karol Maciaszek <karol@maciaszek.pl>

* Update packages/http/src/validator/validators/params.ts

Co-authored-by: Karol Maciaszek <karol@maciaszek.pl>

Co-authored-by: Karol Maciaszek <karol@maciaszek.pl>
  • Loading branch information
XVincentX and karol-maciaszek authored Jul 1, 2020
1 parent 8a40326 commit e5aa564
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ jobs:
name: Build all code to JavaScript
command: yarn build
- run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
- run:
name: Publish on the beta tag
command: yarn lerna publish $(npx semver --preid beta -i prerelease $(npm show @stoplight/prism-cli@beta version)) --no-push --no-git-tag-version --dist-tag beta -y
- run:
name: Publish
command: yarn lerna publish from-git --create-release=github --yes
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

# Unreleased

## Fixed

- Prism is not returning an error anymore when trying to construct a schema for HTTP headers and query string with mixed cases property names [#1268](https://github.com/stoplightio/prism/pull/1268)

## Changed

- **BREAKING**: The `getHttpOperationsFromSpec` has been moved from the HTTP Package to the CLI package. If you're using Prism programmatically, this might require some code changes on your side. `getHttpOperationsFromResource` has been removed. [#1009](https://github.com/stoplightio/prism/pull/1009), [#1192](https://github.com/stoplightio/prism/pull/1192)
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
"posttest": "yarn lint",
"test": "jest",
"release": "lerna version",
"prerelease:beta": "yarn build",
"release:beta": "lerna publish --preid beta --force-publish=* --no-push --no-git-tag-version --dist-tag beta",
"prebuild.binary": "yarn build",
"build.binary": "npx pkg --output ./cli-binaries/prism-cli ./packages/cli/",
"test.harness": "jest -c ./jest.harness.config.js"
Expand Down
38 changes: 18 additions & 20 deletions packages/http/src/validator/validators/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DiagnosticSeverity, HttpParamStyles, IHttpParam } from '@stoplight/type
import { compact, keyBy, mapKeys, mapValues, pickBy, upperFirst } from 'lodash';
import * as E from 'fp-ts/lib/Either';
import * as O from 'fp-ts/lib/Option';
import { fromArray } from 'fp-ts/lib/NonEmptyArray';
import * as NEA from 'fp-ts/lib/NonEmptyArray';
import { pipe } from 'fp-ts/lib/pipeable';
import { JSONSchema4 } from 'json-schema';
import { JSONSchema } from '../../';
Expand Down Expand Up @@ -31,8 +31,9 @@ export class HttpParamsValidator<Target> implements IHttpValidator<Target, IHttp
}));

return pipe(
createJsonSchemaFromParams(specs),
O.map(schema => {
NEA.fromArray(specs),
O.map(specs => {
const schema = createJsonSchemaFromParams(specs);
const parameterValues = pickBy(
mapValues(
keyBy(specs, s => s.name.toLowerCase()),
Expand All @@ -47,7 +48,7 @@ export class HttpParamsValidator<Target> implements IHttpValidator<Target, IHttp
// the validators a bit
// @ts-ignore
mapKeys(target, (_value, key) => key.toLowerCase()),
schema.properties && (schema.properties[el.name] as JSONSchema4),
schema.properties && (schema.properties[el.name.toLowerCase()] as JSONSchema4),
el.explode || false
);

Expand All @@ -59,26 +60,23 @@ export class HttpParamsValidator<Target> implements IHttpValidator<Target, IHttp
}),
O.chain(({ parameterValues, schema }) => validateAgainstSchema(parameterValues, schema, true, prefix)),
O.map(schemaDiagnostic => schemaDiagnostic.concat(deprecatedWarnings)),
O.chain(fromArray),
O.alt(() => fromArray(deprecatedWarnings)),
O.chain(NEA.fromArray),
O.alt(() => NEA.fromArray(deprecatedWarnings)),
E.fromOption(() => target),
E.swap
);
}
}

function createJsonSchemaFromParams(params: IHttpParam[]): O.Option<JSONSchema> {
return pipe(
fromArray(params),
O.map(params => ({
type: 'object',
properties: pickBy(
mapValues(
keyBy(params, p => p.name.toLowerCase()),
'schema'
)
) as JSONSchema4,
required: compact(params.map(m => (m.required ? m.name.toLowerCase() : undefined))),
}))
);
function createJsonSchemaFromParams(params: NEA.NonEmptyArray<IHttpParam>): JSONSchema {
return {
type: 'object',
properties: pickBy(
mapValues(
keyBy(params, p => p.name.toLocaleLowerCase()),
'schema'
)
) as JSONSchema4,
required: compact(params.map(m => (m.required ? m.name.toLowerCase() : undefined))),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
====test====
When I send a request to an operation
And the operation has a query param specified in mixed between lowercase and uppercase
And in the request I sent that param is present in a different casing
It should still pass the validation
====spec====
swagger: '2.0'
produces:
- application/json
paths:
"/v1/test":
get:
parameters:
- collectionFormat: csv
in: query
items:
format: uuid
type: string
name: filter[somethingId]
required: false
type: array
responses:
'200':
description: OK
examples:
error: false
====server====
mock -p 4010 ${document}
====command====
curl -sIXGET http://127.0.0.1:4010/v1/test?somethingid=04fc0172-1943-4c38-8e0f-36935e05b8dd
====expect====
HTTP/1.1 200 OK

0 comments on commit e5aa564

Please sign in to comment.