Skip to content

fix(parser): handle JSON pointers #2223

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion packages/openapi-ts-tests/test/openapi-ts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default defineConfig(() => {
// 'invalid',
// 'servers-entry.yaml',
// ),
path: path.resolve(__dirname, 'spec', '3.1.x', 'content-types.yaml'),
path: path.resolve(__dirname, 'spec', '3.1.x', 'bug.yaml'),
// path: path.resolve(__dirname, 'spec', 'v3-transforms.json'),
// path: 'http://localhost:4000/',
// path: 'https://get.heyapi.dev/',
Expand Down
22 changes: 22 additions & 0 deletions packages/openapi-ts-tests/test/spec/3.1.x/bug.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
openapi: 3.1.0
info:
title: Sample API
version: 1.0.0
paths:
/foo:
get:
responses:
'200':
description: OK
content:
application/json:
schema:
type: string
post:
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/paths/~1foo/get/responses/200/content/application~1json/schema'
4 changes: 4 additions & 0 deletions packages/openapi-ts/src/openApi/3.1.x/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const parseV3_1_X = (context: IR.Context<OpenApiV3_1_X>) => {
for (const name in context.spec.components.securitySchemes) {
const securityOrReference =
context.spec.components.securitySchemes[name]!;
// TODO: safe pointer to path
const securitySchemeObject =
'$ref' in securityOrReference
? context.resolveRef<SecuritySchemeObject>(securityOrReference.$ref)
Expand All @@ -68,6 +69,7 @@ export const parseV3_1_X = (context: IR.Context<OpenApiV3_1_X>) => {
for (const name in context.spec.components.parameters) {
const $ref = `#/components/parameters/${name}`;
const parameterOrReference = context.spec.components.parameters[name]!;
// TODO: safe pointer to path
const parameter =
'$ref' in parameterOrReference
? context.resolveRef<ParameterObject>(parameterOrReference.$ref)
Expand All @@ -84,6 +86,7 @@ export const parseV3_1_X = (context: IR.Context<OpenApiV3_1_X>) => {
const $ref = `#/components/requestBodies/${name}`;
const requestBodyOrReference =
context.spec.components.requestBodies[name]!;
// TODO: safe pointer to path
const requestBody =
'$ref' in requestBodyOrReference
? context.resolveRef<RequestBodyObject>(requestBodyOrReference.$ref)
Expand Down Expand Up @@ -113,6 +116,7 @@ export const parseV3_1_X = (context: IR.Context<OpenApiV3_1_X>) => {
for (const path in context.spec.paths) {
const pathItem = context.spec.paths[path as keyof PathsObject]!;

// TODO: safe pointer to path
const finalPathItem = pathItem.$ref
? {
...context.resolveRef<PathItemObject>(pathItem.$ref),
Expand Down
11 changes: 10 additions & 1 deletion packages/openapi-ts/src/utils/ref.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const jsonPointerSlash = /~1/g;
const jsonPointerTilde = /~0/g;

export const irRef = '#/ir/';

export const isRefOpenApiComponent = ($ref: string): boolean => {
Expand All @@ -20,7 +23,11 @@ export const refToName = ($ref: string): string => {
const refToParts = ($ref: string): string[] => {
// Remove the leading `#` and split by `/` to traverse the object
const parts = $ref.replace(/^#\//, '').split('/');
return parts;
const cleanParts = parts.map((part) =>
part.replace(jsonPointerSlash, '/').replace(jsonPointerTilde, '~'),
);
console.log($ref, cleanParts);
return cleanParts;
};

export const resolveRef = <T>({
Expand All @@ -36,8 +43,10 @@ export const resolveRef = <T>({

let current = spec;

console.log(spec.paths['/foo'].get.responses['200']);
for (const part of parts) {
const p = part as keyof typeof current;
console.log(p);
if (current[p] === undefined) {
throw new Error(`Reference not found: ${$ref}`);
}
Expand Down
Loading