Skip to content

Commit

Permalink
fix: parentId is sometimes invalid for shared components (#193)
Browse files Browse the repository at this point in the history
* fix: parentId is sometimes invalid for shared components

* test: one more level
  • Loading branch information
P0lip authored Jun 6, 2022
1 parent 1d91d4f commit 18f35d9
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export function createContext<T extends Record<string, unknown>>(
return resolveRef.call(this, target);
} catch {
return target;
} finally {
this.parentId = this.ids[context];
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/oas/__tests__/__snapshots__/operation.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ Array [
],
"type": "object",
"x-stoplight": Object {
"id": "b3c54fc227df9",
"id": "d1d03adc8237d",
},
},
},
Expand All @@ -603,7 +603,7 @@ Array [
],
"type": "object",
"x-stoplight": Object {
"id": "b3c54fc227df9",
"id": "d1d03adc8237d",
},
},
},
Expand Down Expand Up @@ -743,7 +743,7 @@ Array [
],
"type": "object",
"x-stoplight": Object {
"id": "849b52cd05782",
"id": "2b4cfd51fbcde",
},
"xml": Object {
"name": "Pet",
Expand Down Expand Up @@ -799,7 +799,7 @@ Array [
],
"type": "object",
"x-stoplight": Object {
"id": "849b52cd05782",
"id": "2b4cfd51fbcde",
},
"xml": Object {
"name": "Pet",
Expand Down Expand Up @@ -1020,7 +1020,7 @@ Array [
],
"type": "object",
"x-stoplight": Object {
"id": "849b52cd05782",
"id": "2b4cfd51fbcde",
},
"xml": Object {
"name": "Pet",
Expand Down Expand Up @@ -1076,7 +1076,7 @@ Array [
],
"type": "object",
"x-stoplight": Object {
"id": "849b52cd05782",
"id": "2b4cfd51fbcde",
},
"xml": Object {
"name": "Pet",
Expand Down Expand Up @@ -1139,7 +1139,7 @@ Array [
],
"type": "object",
"x-stoplight": Object {
"id": "b9501a9ded2c5",
"id": "d1d03adc8237d",
},
},
},
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
NOTE that if any object anywhere ever has an `x-stoplight-id` on it, prefer that
NOTE that if any object anywhere ever has an `x-stoplight.id` on it, prefer that
over calling the generate function.
Used https://md5calc.com/hash/fnv1a32 to hash the ids.
Expand Down
66 changes: 66 additions & 0 deletions src/oas3/__tests__/__fixtures__/shared-components/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"x-stoplight": {
"id": "service_abc"
},
"openapi": "3.0.3",
"info": {
"version": "0.0.3",
"title": "GitHub v3 REST API",
"description": "GitHub's v3 REST API."
},
"paths": {
"/orgs/{org}/repos": {
"get": {
"summary": "Get a organization repository",
"responses": {
"403": {
"$ref": "#/components/responses/forbidden"
}
}
}
}
},
"components": {
"schemas": {
"basic-error": {
"title": "Basic Error",
"description": "Basic Error",
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
},
"rate-limit": {
"title": "Rate Limit",
"description": "The number of allowed requests in the current period",
"type": "integer"
}
},
"headers": {
"X-Rate-Limit": {
"schema": {
"$ref": "#/components/schemas/rate-limit"
}
}
},
"responses": {
"forbidden": {
"headers": {
"X-Rate-Limit": {
"$ref": "#/components/headers/X-Rate-Limit"
}
},
"description": "Forbidden",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/basic-error"
}
}
}
}
}
}
}
105 changes: 105 additions & 0 deletions src/oas3/__tests__/__fixtures__/shared-components/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
NOTE that if any object anywhere ever has a `x-stoplight.id` on it, prefer that
over calling the generate function.
*/
export default [
/**
* The http_service
*/
{
// hash(document id - end user needs to be able to customize this.)
// this example has a x-stoplight.id prop on the root though, so using that
id: 'service_abc',
version: '0.0.3',
name: 'GitHub v3 REST API',
description: "GitHub's v3 REST API.",
},

/**
* http_operation 1 of 1 (the GET operation)
*/
{
// hash(`http_operation-${parentId}-${method}-${pathWithParamNamesEmpty}`)
// for pathWithParamNamesEmpty, remove all characters between {} segments
// closest parent with an id is the service, so ends up being...
// hash('http_operation-service_abc-get-/orgs/{}/repos')
id: '376a534068842',
method: 'get',
path: '/orgs/{org}/repos',
summary: 'Get a organization repository',
responses: [
{
// hash(`http_response-${parentId}-${response.code || response key (for shared response)}`)
// closest parent with an id is the operation, so ends up being...
// hash('http_response-service_abc-forbidden')
id: 'c73bfcb376d49',
code: '403',
description: 'Forbidden',
headers: [
{
// hash(`http_header-${parentId}-${header key}`)
// it's a shared header, so the closest parent is the service.
// hash('http_header-service_abc-X-Rate-Limit')
id: '96620a275464f',
name: 'X-Rate-Limit',
style: 'simple',
encodings: [],
examples: [],
schema: {
$schema: 'http://json-schema.org/draft-07/schema#',
// hash(`http_header-${parentId}-${header key}`)
// it's a shared header, so the closest parent is the service.
// hash('schema-service_abc-rate-limit')
'x-stoplight': {
id: '72da152ada960',
},
type: 'integer',
description: 'The number of allowed requests in the current period',
title: 'Rate Limit',
},
},
],
contents: [
{
// hash(`http_media-${parentId}-${mediaType}`)
// closest parent with an id is the response, so ends up being...
// hash('http_media-c73bfcb376d49-application/json')
id: '4143bd61bfef9',
mediaType: 'application/json',
schema: {
$schema: 'http://json-schema.org/draft-07/schema#',
'x-stoplight': {
// hash('schema-service_abc-basic-error')
id: '5cbb77597a983',
},
title: 'Basic Error',
description: 'Basic Error',
type: 'object',
properties: {
message: {
type: 'string',
},
},
},
examples: [],
encodings: [],
},
],
},
],
servers: [],
request: {
body: {
id: '913ab62a764b4',
contents: [],
},
headers: [],
query: [],
cookie: [],
path: [],
},
tags: [],
security: [],
extensions: {},
},
];
15 changes: 10 additions & 5 deletions src/oas3/__tests__/ids.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import * as path from 'path';
import { transformOas3Operations } from '../operation';
import { transformOas3Service } from '../service';

test('should generate proper ids', async () => {
const document = JSON.parse(await fs.promises.readFile(path.join(__dirname, '../__fixtures__/id.json'), 'utf8'));
const { default: output } = await import('../__fixtures__/output');
test.each(fs.readdirSync(path.join(__dirname, './__fixtures__')))(
'given %s, should generate proper ids',
async name => {
const document = JSON.parse(
await fs.promises.readFile(path.join(__dirname, './__fixtures__', name, 'input.json'), 'utf8'),
);
const { default: output } = await import(`./__fixtures__/${name}/output`);

expect([transformOas3Service({ document }), ...transformOas3Operations(document)]).toEqual(output);
});
expect([transformOas3Service({ document }), ...transformOas3Operations(document)]).toEqual(output);
},
);

0 comments on commit 18f35d9

Please sign in to comment.