|
| 1 | +import { EmitContext, ModelProperty, Namespace, Program, Scalar } from "@typespec/compiler"; |
| 2 | +import { AssetEmitter } from "@typespec/compiler/emitter-framework"; |
| 3 | +import { MetadataInfo } from "@typespec/http"; |
| 4 | +import { getExternalDocs, resolveInfo } from "@typespec/openapi"; |
| 5 | +import { JsonSchemaModule } from "./json-schema.js"; |
| 6 | +import { OpenAPI3EmitterOptions, OpenAPIVersion } from "./lib.js"; |
| 7 | +import { |
| 8 | + applyEncoding as applyEncoding3_0, |
| 9 | + getRawBinarySchema as getRawBinarySchema3_0, |
| 10 | + isRawBinarySchema as isRawBinarySchema3_0, |
| 11 | +} from "./openapi-helpers-3-0.js"; |
| 12 | +import { |
| 13 | + applyEncoding as applyEncoding3_1, |
| 14 | + getRawBinarySchema as getRawBinarySchema3_1, |
| 15 | + isRawBinarySchema as isRawBinarySchema3_1, |
| 16 | +} from "./openapi-helpers-3-1.js"; |
| 17 | +import { ResolvedOpenAPI3EmitterOptions } from "./openapi.js"; |
| 18 | +import { createSchemaEmitter3_0 } from "./schema-emitter-3-0.js"; |
| 19 | +import { createSchemaEmitter3_1 } from "./schema-emitter-3-1.js"; |
| 20 | +import { OpenAPI3Schema, OpenAPISchema3_1, SupportedOpenAPIDocuments } from "./types.js"; |
| 21 | +import { VisibilityUsageTracker } from "./visibility-usage.js"; |
| 22 | +import { XmlModule } from "./xml-module.js"; |
| 23 | + |
| 24 | +export type CreateSchemaEmitter = (props: { |
| 25 | + program: Program; |
| 26 | + context: EmitContext<OpenAPI3EmitterOptions>; |
| 27 | + metadataInfo: MetadataInfo; |
| 28 | + visibilityUsage: VisibilityUsageTracker; |
| 29 | + options: ResolvedOpenAPI3EmitterOptions; |
| 30 | + optionalDependencies: { jsonSchemaModule?: JsonSchemaModule; xmlModule?: XmlModule }; |
| 31 | +}) => AssetEmitter<OpenAPI3Schema | OpenAPISchema3_1, OpenAPI3EmitterOptions>; |
| 32 | + |
| 33 | +export interface OpenApiSpecSpecificProps { |
| 34 | + applyEncoding( |
| 35 | + program: Program, |
| 36 | + typespecType: Scalar | ModelProperty, |
| 37 | + target: OpenAPI3Schema, |
| 38 | + options: ResolvedOpenAPI3EmitterOptions, |
| 39 | + ): OpenAPI3Schema & OpenAPISchema3_1; |
| 40 | + createRootDoc: ( |
| 41 | + program: Program, |
| 42 | + serviceType: Namespace, |
| 43 | + serviceVersion?: string, |
| 44 | + ) => SupportedOpenAPIDocuments; |
| 45 | + |
| 46 | + createSchemaEmitter: CreateSchemaEmitter; |
| 47 | + /** |
| 48 | + * Returns the binary description for an unencoded binary type |
| 49 | + * @see https://spec.openapis.org/oas/v3.1.1.html#migrating-binary-descriptions-from-oas-3-0 |
| 50 | + */ |
| 51 | + getRawBinarySchema(contentType?: string): OpenAPI3Schema | OpenAPISchema3_1; |
| 52 | + |
| 53 | + isRawBinarySchema(schema: OpenAPI3Schema | OpenAPISchema3_1): boolean; |
| 54 | +} |
| 55 | + |
| 56 | +export function getOpenApiSpecProps(specVersion: OpenAPIVersion): OpenApiSpecSpecificProps { |
| 57 | + switch (specVersion) { |
| 58 | + case "3.0.0": |
| 59 | + return { |
| 60 | + applyEncoding: applyEncoding3_0, |
| 61 | + createRootDoc(program, serviceType, serviceVersion) { |
| 62 | + return createRoot(program, serviceType, specVersion, serviceVersion); |
| 63 | + }, |
| 64 | + createSchemaEmitter: createSchemaEmitter3_0, |
| 65 | + getRawBinarySchema: getRawBinarySchema3_0, |
| 66 | + isRawBinarySchema: isRawBinarySchema3_0, |
| 67 | + }; |
| 68 | + case "3.1.0": |
| 69 | + return { |
| 70 | + applyEncoding: applyEncoding3_1, |
| 71 | + createRootDoc(program, serviceType, serviceVersion) { |
| 72 | + return createRoot(program, serviceType, specVersion, serviceVersion); |
| 73 | + }, |
| 74 | + createSchemaEmitter: createSchemaEmitter3_1, |
| 75 | + getRawBinarySchema: getRawBinarySchema3_1, |
| 76 | + isRawBinarySchema: isRawBinarySchema3_1, |
| 77 | + }; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function createRoot( |
| 82 | + program: Program, |
| 83 | + serviceType: Namespace, |
| 84 | + specVersion: OpenAPIVersion, |
| 85 | + serviceVersion?: string, |
| 86 | +): SupportedOpenAPIDocuments { |
| 87 | + const info = resolveInfo(program, serviceType); |
| 88 | + |
| 89 | + return { |
| 90 | + openapi: specVersion, |
| 91 | + info: { |
| 92 | + title: "(title)", |
| 93 | + ...info, |
| 94 | + version: serviceVersion ?? info?.version ?? "0.0.0", |
| 95 | + }, |
| 96 | + externalDocs: getExternalDocs(program, serviceType), |
| 97 | + tags: [], |
| 98 | + paths: {}, |
| 99 | + security: undefined, |
| 100 | + components: { |
| 101 | + parameters: {}, |
| 102 | + requestBodies: {}, |
| 103 | + responses: {}, |
| 104 | + schemas: {}, |
| 105 | + examples: {}, |
| 106 | + securitySchemes: {}, |
| 107 | + }, |
| 108 | + }; |
| 109 | +} |
0 commit comments