Skip to content

Commit

Permalink
add componentRef specification
Browse files Browse the repository at this point in the history
  • Loading branch information
inkognitro committed Mar 24, 2024
1 parent d8168f9 commit 885da71
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/oas3/codegen/ts/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export function applyEndpointCallerFunction(
path,
requiredOutputPaths: [
endpointIdConstDefinition.path,
templateRequestType.path,
requestResultTypeDefinition.path,
templateRequestHandlerType.path,
],
Expand Down
5 changes: 3 additions & 2 deletions src/oas3/codegen/ts/response.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
isComponentRef,
isResponseComponentRef,
ResponseBodyContent,
ResponseByStatusCodeMap,
} from '@oas3/specification';
Expand Down Expand Up @@ -61,7 +61,7 @@ export function applyResponseByStatusCodeMap(
for (const statusCode in schema) {
const responseOutputPath: OutputPath = [...path, statusCode];
const responseOrRef = schema[statusCode];
if (isComponentRef(responseOrRef)) {
if (isResponseComponentRef(responseOrRef)) {
const responseOutput = applyComponentRefSchema(
codeGenerator,
responseOrRef,
Expand Down Expand Up @@ -107,6 +107,7 @@ export function applyResponseByStatusCodeMap(
},
requiredOutputPaths: responseOutputs.map(o => o.path),
};
codeGenerator.addIndirectOutput(responseTypeDefinition);
return {
path,
requiredOutputPaths: [responseTypeDefinition.path],
Expand Down
6 changes: 3 additions & 3 deletions src/oas3/codegen/ts/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import {
ComponentRef,
isArraySchema,
isBooleanSchema,
isComponentRef,
isNumberSchema,
isObjectSchema,
isOneOfSchema,
isSchemaComponentRef,
isStringSchema,
NumberSchema,
ObjectSchema,
Expand All @@ -33,7 +33,7 @@ export function applySchema(
schema: Schema,
path: OutputPath
): CodeGenerationOutput {
if (isComponentRef(schema)) {
if (isSchemaComponentRef(schema)) {
return applyComponentRefSchema(codeGenerator, schema, path);
}
if (isBooleanSchema(schema)) {
Expand Down Expand Up @@ -350,7 +350,7 @@ export function applyOneOfSchema(
itemPath,
objectDiscriminatorConfig
);
} else if (isComponentRef(itemSchema)) {
} else if (isSchemaComponentRef(itemSchema)) {
itemOutput = applyComponentRefSchema(
codeGenerator,
itemSchema,
Expand Down
38 changes: 38 additions & 0 deletions src/oas3/specification/componentRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,41 @@ export function isComponentRef(anyValue: any): anyValue is ComponentRef {
}
return true;
}

export function isSchemaComponentRef(anyValue: any): anyValue is ComponentRef {
const value = anyValue as ComponentRef;
if (!isComponentRef(value)) {
return false;
}
return value.$ref.startsWith('#/components/schemas/');
}

export function isResponseComponentRef(
anyValue: any
): anyValue is ComponentRef {
const value = anyValue as ComponentRef;
if (!isComponentRef(value)) {
return false;
}
return value.$ref.startsWith('#/components/responses/');
}

export function isParameterComponentRef(
anyValue: any
): anyValue is ComponentRef {
const value = anyValue as ComponentRef;
if (!isComponentRef(value)) {
return false;
}
return value.$ref.startsWith('#/components/parameters/');
}

export function isSecuritySchemesComponentRef(
anyValue: any
): anyValue is ComponentRef {
const value = anyValue as ComponentRef;
if (!isComponentRef(value)) {
return false;
}
return value.$ref.startsWith('#/components/securitySchemes/');
}
4 changes: 2 additions & 2 deletions src/oas3/specification/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {isSchema, Schema} from './schema';
import {isResponseByStatusCodeMap, ResponseByStatusCodeMap} from './response';
import {ComponentRef, isComponentRef} from './componentRef';
import {ComponentRef, isParameterComponentRef} from './componentRef';

type PermissionsBySecurityName = {
[securityName: string]: string[];
Expand Down Expand Up @@ -145,7 +145,7 @@ export function isRequest(anyValue: any): anyValue is Request {
return false;
}
const invalidParameter = value.parameters?.find(
p => !isRequestParameter(p) && !isComponentRef(p)
p => !isRequestParameter(p) && !isParameterComponentRef(p)
);
if (invalidParameter) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/oas3/specification/response.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {isSchema, Schema} from './schema';
import {ComponentRef, isComponentRef} from './componentRef';
import {ComponentRef, isResponseComponentRef} from './componentRef';

export type ResponseBodyContent = {
schema: Schema;
Expand Down Expand Up @@ -53,7 +53,7 @@ export function isResponseByStatusCodeMap(
const value = anyValue as ResponseByStatusCodeMap;
for (const statusCode in value) {
const responseOrRef = value[statusCode];
if (!isComponentRef(responseOrRef) && !isResponse(responseOrRef)) {
if (!isResponseComponentRef(responseOrRef) && !isResponse(responseOrRef)) {
return false;
}
}
Expand Down

0 comments on commit 885da71

Please sign in to comment.