Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly lookup the properties in lowercase #1268

Merged
merged 9 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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