|
1 | | -import type { Service, SourceFile } from "@typespec/compiler"; |
2 | | -import type { OpenAPI2Document } from "./openapi2-document.js"; |
| 1 | +import { getResourceFeature } from "@azure-tools/typespec-azure-resource-manager"; |
| 2 | +import { |
| 3 | + compilerAssert, |
| 4 | + Program, |
| 5 | + type ModelProperty, |
| 6 | + type Operation, |
| 7 | + type Service, |
| 8 | + type SourceFile, |
| 9 | + type Type, |
| 10 | +} from "@typespec/compiler"; |
| 11 | +import { TwoLevelMap } from "@typespec/compiler/utils"; |
| 12 | +import { Visibility } from "@typespec/http"; |
| 13 | +import type { |
| 14 | + OpenAPI2Document, |
| 15 | + OpenAPI2Parameter, |
| 16 | + OpenAPI2PathItem, |
| 17 | + OpenAPI2Schema, |
| 18 | + Refable, |
| 19 | +} from "./openapi2-document.js"; |
3 | 20 |
|
4 | 21 | /** |
5 | 22 | * A record containing the the OpenAPI 3 documents corresponding to |
@@ -53,10 +70,164 @@ export interface AutorestEmitterResult { |
53 | 70 |
|
54 | 71 | /** Output file used */ |
55 | 72 | readonly outputFile: string; |
| 73 | + |
| 74 | + /** The feature associated with this file, if any */ |
| 75 | + readonly feature?: string; |
56 | 76 | } |
57 | 77 |
|
58 | 78 | export interface LoadedExample { |
59 | 79 | readonly relativePath: string; |
60 | 80 | readonly file: SourceFile; |
61 | 81 | readonly data: any; |
62 | 82 | } |
| 83 | + |
| 84 | +/** |
| 85 | + * Represents a node that will hold a JSON reference. The value is computed |
| 86 | + * at the end so that we can defer decisions about the name that is |
| 87 | + * referenced. |
| 88 | + */ |
| 89 | +export class LateBoundReference { |
| 90 | + isLocal?: boolean; |
| 91 | + file?: string; |
| 92 | + value?: string; |
| 93 | + setLocalValue(program: Program, inValue: string, type?: Type): void { |
| 94 | + if (type) { |
| 95 | + switch (type.kind) { |
| 96 | + case "Model": |
| 97 | + this.file = getResourceFeature(program, type); |
| 98 | + break; |
| 99 | + default: |
| 100 | + this.file = "common"; |
| 101 | + } |
| 102 | + } |
| 103 | + this.isLocal = true; |
| 104 | + this.value = inValue; |
| 105 | + } |
| 106 | + setRemoteValue(inValue: string): void { |
| 107 | + this.isLocal = false; |
| 108 | + this.value = inValue; |
| 109 | + } |
| 110 | + toJSON() { |
| 111 | + compilerAssert(this.value, "Reference value never set."); |
| 112 | + if (!this.isLocal) return this.value; |
| 113 | + if (this.file === undefined) return `#/definitions/${this.value}`; |
| 114 | + return `${this.file}/definitions/${this.value}`; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Represents a non-inlined schema that will be emitted as a definition. |
| 120 | + * Computation of the OpenAPI schema object is deferred. |
| 121 | + */ |
| 122 | +export interface PendingSchema { |
| 123 | + /** The TYPESPEC type for the schema */ |
| 124 | + type: Type; |
| 125 | + |
| 126 | + /** The visibility to apply when computing the schema */ |
| 127 | + visibility: Visibility; |
| 128 | + |
| 129 | + /** |
| 130 | + * The JSON reference to use to point to this schema. |
| 131 | + * |
| 132 | + * Note that its value will not be computed until all schemas have been |
| 133 | + * computed as we will add a suffix to the name if more than one schema |
| 134 | + * must be emitted for the type for different visibilities. |
| 135 | + */ |
| 136 | + ref: LateBoundReference; |
| 137 | + |
| 138 | + /** |
| 139 | + * Determines the schema name if an override has been set |
| 140 | + * @param name The default name of the schema |
| 141 | + * @param visibility The visibility in which the schema is used |
| 142 | + * @returns The name of the given schema in the given visibility context |
| 143 | + */ |
| 144 | + getSchemaNameOverride?: (name: string, visibility: Visibility) => string; |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * Represents a schema that is ready to emit as its OpenAPI representation |
| 149 | + * has been produced. |
| 150 | + */ |
| 151 | +export interface ProcessedSchema extends PendingSchema { |
| 152 | + schema: OpenAPI2Schema | undefined; |
| 153 | +} |
| 154 | + |
| 155 | +/** Abstracts away methods to create a OpenAPI 2.0 document ragardless of layout. */ |
| 156 | +export interface OpenApi2DocumentProxy { |
| 157 | + /** |
| 158 | + * Resolve the logical OpenAPI document into a set of emitter results |
| 159 | + */ |
| 160 | + resolveDocuments(): Promise<AutorestEmitterResult[]>; |
| 161 | + /** |
| 162 | + * Get the parameters for an operation |
| 163 | + * @param op The operation to get parameters for |
| 164 | + */ |
| 165 | + getParameters(op: Operation): Map<ModelProperty, OpenAPI2Parameter>; |
| 166 | + /** Add a tag to an operation |
| 167 | + * @param op The operation to add a tag to |
| 168 | + * @param tag The tag to add |
| 169 | + */ |
| 170 | + addTag(op: Operation, tag: string): void; |
| 171 | + /** |
| 172 | + * Add a produces MIME type to an operation |
| 173 | + * @param op The operation to add the produces MIME type to |
| 174 | + * @param produces The MIME type to add |
| 175 | + */ |
| 176 | + addProduces(op: Operation, produces: string): void; |
| 177 | + |
| 178 | + /** |
| 179 | + * Add a consumes MIME type to an operation |
| 180 | + * @param op The operation to add the consumes MIME type to |
| 181 | + * @param consumes The MIME type to add |
| 182 | + */ |
| 183 | + addConsumes(op: Operation, consumes: string): void; |
| 184 | + |
| 185 | + /** |
| 186 | + * Add examples to an operation |
| 187 | + * @param op The operation to add examples to |
| 188 | + * @param examples The examples to add |
| 189 | + */ |
| 190 | + addExamples(op: Operation, examples: LoadedExample[]): void; |
| 191 | + |
| 192 | + /** |
| 193 | + * get the tags for an operation |
| 194 | + * @param op The operation to get tags for |
| 195 | + */ |
| 196 | + getTags(op: Operation): Set<string>; |
| 197 | + |
| 198 | + /** |
| 199 | + * Get the consumes MIME types for an operation |
| 200 | + * @param op The operation to get consumes MIME types for |
| 201 | + */ |
| 202 | + getConsumes(op: Operation): Set<string>; |
| 203 | + |
| 204 | + /** |
| 205 | + * Get the produces MIME types for an operation |
| 206 | + * @param op The operation to get produces MIME types for |
| 207 | + */ |
| 208 | + getProduces(op: Operation): Set<string>; |
| 209 | + |
| 210 | + /** |
| 211 | + * Get or add the path associated with the given operation |
| 212 | + * @param op The operation to get or add the path for |
| 213 | + */ |
| 214 | + createOrAddPathItem(op: Operation): OpenAPI2PathItem; |
| 215 | + |
| 216 | + /** |
| 217 | + * Get the file name for a given type |
| 218 | + * @param type The type to get the file name for |
| 219 | + */ |
| 220 | + getFile(type: Type): string | undefined; |
| 221 | + |
| 222 | + /** |
| 223 | + * Get or add a parameter placeholder for a given property |
| 224 | + * @param property The property to get or add the parameter placeholder for |
| 225 | + */ |
| 226 | + getOrAddParamPlaceholder(property: ModelProperty): Refable<OpenAPI2Parameter>; |
| 227 | + |
| 228 | + /** The schemas that are not yet resolved */ |
| 229 | + pendingSchemas: TwoLevelMap<Type, Visibility, PendingSchema>; |
| 230 | + |
| 231 | + /** references included in schemas */ |
| 232 | + refs: TwoLevelMap<Type, Visibility, LateBoundReference>; |
| 233 | +} |
0 commit comments