Skip to content

Commit

Permalink
fix(oas3): example id generation (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Feb 22, 2023
1 parent dcc9fb3 commit 179018d
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/oas/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export function inferContext(path: JsonPath): AvailableContext {

const SHARED_COMPONENTS_KEYS = new WeakMap();

export function getSharedKey(value: object) {
return SHARED_COMPONENTS_KEYS.get(value);
export function getSharedKey(value: object, currentKey: string) {
return SHARED_COMPONENTS_KEYS.get(value) ?? currentKey;
}

export function setSharedKey(value: unknown, key: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/oas/transformers/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const translateSchemaObject = withContext<
>(function (schema) {
const maybeSchemaObject = this.maybeResolveLocalRef(schema);
if (isReferenceObject(maybeSchemaObject)) return maybeSchemaObject;
const actualKey = this.context === 'service' ? getSharedKey(Object(maybeSchemaObject)) : '';
const actualKey = this.context === 'service' ? getSharedKey(Object(maybeSchemaObject), '') : '';
return translateSchemaObjectFromPair.call(this, [actualKey, schema]);
});

Expand Down
6 changes: 3 additions & 3 deletions src/oas2/transformers/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const translateToHeaderParam = withContext<
>
>(function (param) {
const name = param.name;
const keyOrName = getSharedKey(param) ?? name;
const keyOrName = getSharedKey(param, name);

return {
id: this.generateId.httpHeader({ keyOrName }),
Expand Down Expand Up @@ -252,7 +252,7 @@ export const translateToQueryParameter = withContext<
Oas2TranslateFunction<[query: DeepPartial<QueryParameter> & Oas2ParamBase], IHttpQueryParam<true>>
>(function (param) {
const name = param.name;
const keyOrName = getSharedKey(param) ?? name;
const keyOrName = getSharedKey(param, name);

return {
id: this.generateId.httpQuery({ keyOrName }),
Expand All @@ -275,7 +275,7 @@ export const translateToPathParameter = withContext<
Oas2TranslateFunction<[param: DeepPartial<PathParameter> & Oas2ParamBase], IHttpPathParam<true>>
>(function (param) {
const name = param.name;
const keyOrName = getSharedKey(param) ?? name;
const keyOrName = getSharedKey(param, name);

return {
id: this.generateId.httpPathParam({ keyOrName }),
Expand Down
2 changes: 1 addition & 1 deletion src/oas2/transformers/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const translateToResponse = withContext<

if (!isResponseObject(maybeResponseObject)) return;

const codeOrKey = this.context === 'service' ? getSharedKey(maybeResponseObject) : statusCode;
const codeOrKey = this.context === 'service' ? getSharedKey(maybeResponseObject, statusCode) : statusCode;
const id = this.generateId.httpResponse({ codeOrKey, produces });

const headers = translateToHeaderParams.call(this, maybeResponseObject.headers);
Expand Down
93 changes: 93 additions & 0 deletions src/oas3/__tests__/__fixtures__/examples/bundled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
export default {
id: 'service_abc',
name: 'Users API',
version: '1.0',
servers: [
{
id: 'http_server-service_abc-http://localhost:3000',
name: 'Users API',
url: 'http://localhost:3000',
},
],
operations: [
{
extensions: {},
id: 'http_operation-service_abc-post-/users/{}',
method: 'post',
path: '/users/{userId}',
request: {
body: {
id: 'http_request_body-http_operation-service_abc-post-/users/{}',
contents: [
{
encodings: [],
id: 'http_media-http_request_body-http_operation-service_abc-post-/users/{}-application/json',
examples: [
{
id: 'example-http_media-http_request_body-http_operation-service_abc-post-/users/{}-application/json-foo',
key: 'foo',
summary: 'A foo example',
value: {
foo: 'bar',
},
},
{
id: 'example-http_media-http_request_body-http_operation-service_abc-post-/users/{}-application/json-bar',
key: 'bar',
summary: 'A bar example',
value: {
bar: 'baz',
},
},
{
$ref: '#/components/examples/0',
key: 'baz',
},
],
mediaType: 'application/json',
},
],
},
cookie: [],
headers: [],
path: [],
query: [],
},
responses: [],
security: [],
servers: [
{
id: 'http_server-service_abc-http://localhost:3000',
name: 'Users API',
url: 'http://localhost:3000',
},
],
tags: [],
},
],
components: {
cookie: [],
examples: [
{
id: 'example-service_abc-baz',
key: 'baz',
summary: 'A baz example',
value: {
baz: 'qux',
},
},
{
externalValue: 'http://foo.bar/examples/frog-example.json',
id: 'example-service_abc-frog-example',
key: 'frog-example',
},
],
header: [],
path: [],
query: [],
requestBodies: [],
responses: [],
schemas: [],
securitySchemes: [],
},
};
88 changes: 88 additions & 0 deletions src/oas3/__tests__/__fixtures__/examples/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"openapi": "3.1.0",
"x-stoplight": {
"id": "service_abc"
},
"info": {
"title": "Users API",
"version": "1.0"
},
"servers": [
{
"url": "http://localhost:3000"
}
],
"paths": {
"/users/{userId}": {
"post": {
"requestBody": {
"content": {
"application/json": {
"examples": {
"foo": {
"summary": "A foo example",
"value": {
"foo": "bar"
}
},
"bar": {
"summary": "A bar example",
"value": {
"bar": "baz"
}
},
"baz": {
"$ref": "#/components/examples/baz"
}
}
}
}
},
"responses": {
"200": {
"application/json": {
"examples": {
"cat": {
"summary": "An example of a cat",
"value": {
"name": "Fluffy",
"petType": "Cat",
"color": "White",
"gender": "male",
"breed": "Persian"
}
},
"dog": {
"summary": "An example of a dog with a cat's name",
"value": {
"name": "Puma",
"petType": "Dog",
"color": "Black",
"gender": "Female",
"breed": "Mixed"
}
},
"frog": {
"$ref": "#/components/examples/frog-example"
}
}
}
}
}
}
}
},
"components": {
"examples": {
"baz": {
"summary": "A baz example",
"value": {
"baz": "qux"
}
},
"frog-example": {
"externalValue": "http://foo.bar/examples/frog-example.json"
}
}
}
}
72 changes: 72 additions & 0 deletions src/oas3/__tests__/__fixtures__/examples/legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
export default [
{
id: 'service_abc',
name: 'Users API',
servers: [
{
id: 'http_server-service_abc-http://localhost:3000',
name: 'Users API',
url: 'http://localhost:3000',
},
],
version: '1.0',
},
{
id: 'http_operation-service_abc-post-/users/{}',
extensions: {},
method: 'post',
path: '/users/{userId}',
request: {
body: {
id: 'http_request_body-http_operation-service_abc-post-/users/{}',
contents: [
{
id: 'http_media-http_request_body-http_operation-service_abc-post-/users/{}-application/json',
encodings: [],
examples: [
{
id: 'example-http_media-http_request_body-http_operation-service_abc-post-/users/{}-application/json-foo',
key: 'foo',
summary: 'A foo example',
value: {
foo: 'bar',
},
},
{
id: 'example-http_media-http_request_body-http_operation-service_abc-post-/users/{}-application/json-bar',
key: 'bar',
summary: 'A bar example',
value: {
bar: 'baz',
},
},
{
id: 'example-service_abc-baz',
key: 'baz',
summary: 'A baz example',
value: {
baz: 'qux',
},
},
],
mediaType: 'application/json',
},
],
},
cookie: [],
headers: [],
path: [],
query: [],
},
responses: [],
security: [],
servers: [
{
id: 'http_server-service_abc-http://localhost:3000',
name: 'Users API',
url: 'http://localhost:3000',
},
],
tags: [],
},
];
2 changes: 1 addition & 1 deletion src/oas3/transformers/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const translateToExample = withContext<
return maybeExample as { key: string } & Reference;
}

const keyOrName = (this.context === 'service' && getSharedKey(maybeExample)) ?? key;
const keyOrName = this.context === 'service' ? getSharedKey(maybeExample, key) : key;

return {
id: this.generateId.example({ keyOrName }),
Expand Down
2 changes: 1 addition & 1 deletion src/oas3/transformers/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const translateParameterObject = withContext<
>(function (parameterObject) {
const kind = parameterObject.in === 'path' ? 'pathParam' : parameterObject.in;
const name = parameterObject.name;
const keyOrName = getSharedKey(parameterObject) ?? name;
const keyOrName = getSharedKey(parameterObject, name);
const id = this.generateId[`http${kind[0].toUpperCase()}${kind.slice(1)}`]({ keyOrName });
const schema = translateParameterObjectSchema.call(this, parameterObject);

Expand Down
2 changes: 1 addition & 1 deletion src/oas3/transformers/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const translateToResponse = withContext<

if (!isResponseObject(maybeResponseObject)) return;

const codeOrKey = this.context === 'service' ? getSharedKey(maybeResponseObject) : statusCode;
const codeOrKey = this.context === 'service' ? getSharedKey(maybeResponseObject, statusCode) : statusCode;

return {
id: this.generateId.httpResponse({ codeOrKey }),
Expand Down

0 comments on commit 179018d

Please sign in to comment.