Skip to content

Commit

Permalink
feat(oas): augment auto sampling errors with error location info
Browse files Browse the repository at this point in the history
  • Loading branch information
derevnjuk committed Jan 27, 2022
1 parent d63e050 commit c4e68ed
Showing 1 changed file with 79 additions and 21 deletions.
100 changes: 79 additions & 21 deletions packages/oas/src/converter/DefaultConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export class DefaultConverter implements Converter {
method: string
): PostData | null {
const pathObj = spec.paths[path][method];
// TODO: escape a reference token (i.g. path)
const jsonPointer = `/paths/${path}/${method}`;

for (const param of pathObj.parameters || []) {
if (
Expand All @@ -133,10 +135,18 @@ export class DefaultConverter implements Converter {
typeof param.schema !== 'undefined'
) {
try {
const data = this.sample(param.schema, {
spec,
skipReadOnly: true
});
const data = this.sample(
param.schema,
{
skipReadOnly: true
},
{
spec,
jsonPointer: `${jsonPointer}/parameters/${pathObj.parameters.indexOf(
param
)}/schema`
}
);

let consumes;

Expand Down Expand Up @@ -174,10 +184,17 @@ export class DefaultConverter implements Converter {

if (content[contentType] && content[contentType].schema) {
const sampleContent = content[contentType];
const data = this.sample(content[contentType].schema, {
spec,
skipReadOnly: true
});
const data = this.sample(
content[contentType].schema,
{
skipReadOnly: true
},
{
spec,
// TODO: escape a reference token (i.g. contentType)
jsonPointer: `${jsonPointer}/requestBody/content/${contentType}/schema`
}
);

return this.encodePayload(data, contentType, sampleContent.encoding);
}
Expand Down Expand Up @@ -337,17 +354,29 @@ export class DefaultConverter implements Converter {
values: Record<string, string> = {}
): QueryString[] {
const queryStrings: QueryString[] = [];
const pathObj = spec.paths[path][method];
// TODO: escape a reference token (i.g. path)
const jsonPointer = `/paths/${path}/${method}`;

if (typeof spec.paths[path][method].parameters === 'undefined') {
if (typeof pathObj.parameters === 'undefined') {
return queryStrings;
}

for (const param of spec.paths[path][method].parameters) {
for (const param of pathObj.parameters) {
if (
typeof param.in !== 'undefined' &&
param.in.toLowerCase() === 'query'
) {
const data = this.sample(param.schema || param, { spec });
const data = this.sample(
param.schema || param,
{},
{
spec,
jsonPointer: `${jsonPointer}/parameters/${pathObj.parameters.indexOf(
param
)}/schema`
}
);

if (typeof values[param.name] !== 'undefined') {
queryStrings.push({
Expand Down Expand Up @@ -385,8 +414,9 @@ export class DefaultConverter implements Converter {
method: string
): Header[] {
const headers: Header[] = [];

const pathObj = spec.paths[path][method];
// TODO: escape a reference token (i.g. path)
const jsonPointer = `/paths/${path}/${method}`;

// 'content-type' header:
if (typeof pathObj.consumes !== 'undefined') {
Expand Down Expand Up @@ -425,7 +455,16 @@ export class DefaultConverter implements Converter {
typeof param.in !== 'undefined' &&
param.in.toLowerCase() === 'header'
) {
const data = this.sample(param.schema || param, { spec });
const data = this.sample(
param.schema || param,
{},
{
spec,
jsonPointer: `${jsonPointer}/parameters/${pathObj.parameters.indexOf(
param
)}/schema`
}
);
headers.push({
name: param.name.toLowerCase(),
value: typeof data === 'object' ? JSON.stringify(data) : data
Expand Down Expand Up @@ -614,11 +653,23 @@ export class DefaultConverter implements Converter {
): string {
const templateUrl = template.parse(path);
const params = {};
const pathObj = spec.paths[path][method];
// TODO: escape a reference token (i.g. path)
const jsonPointer = `/paths/${path}/${method}`;

if (typeof spec.paths[path][method].parameters !== 'undefined') {
for (const param of spec.paths[path][method].parameters) {
if (typeof pathObj.parameters !== 'undefined') {
for (const param of pathObj.parameters) {
if (param?.in.toLowerCase() === 'path') {
const data = this.sample(param.schema || param, { spec });
const data = this.sample(
param.schema || param,
{},
{
spec,
jsonPointer: `${jsonPointer}/parameters/${pathObj.parameters.indexOf(
param
)}${param.schema ? '/schema' : ''}`
}
);
Object.assign(params, { [param.name]: data });
}
}
Expand Down Expand Up @@ -653,13 +704,18 @@ export class DefaultConverter implements Converter {

private parseUrls(spec: OpenAPI.Document): string[] {
if (this.isOASV3(spec) && spec.servers?.length) {
return spec.servers.map((server: OpenAPIV3.ServerObject) => {
return spec.servers.map((server: OpenAPIV3.ServerObject, idx: number) => {
const variables = server.variables || {};
const templateUrl = template.parse(server.url);
const params = {};

for (const [param, variable] of Object.entries(variables)) {
const data = this.sample(variable, { spec });
const data = this.sample(
variable,
{},
// TODO: escape a reference token (i.g. param)
{ spec, jsonPointer: `/servers/${idx}/variables/${param}` }
);
Object.assign(params, { [param]: data });
}

Expand All @@ -686,14 +742,16 @@ export class DefaultConverter implements Converter {

private sample(
schema: Schema,
options?: {
options?: Options,
context?: {
spec?: OpenAPI.Document;
jsonPointer?: string;
} & Options
): any | undefined {
try {
return sample(schema, options, options?.spec);
return sample(schema, options, context?.spec);
} catch (e) {
throw new ConvertError(e.message);
throw new ConvertError(e.message, context?.jsonPointer);
}
}

Expand Down

0 comments on commit c4e68ed

Please sign in to comment.