@typespec/http
Advanced tools
Comparing version 0.59.0-dev.5 to 0.59.0-dev.6
@@ -1,2 +0,2 @@ | ||
import { DiagnosticResult, Type, type Diagnostic, type ModelProperty, type Program } from "@typespec/compiler"; | ||
import { DiagnosticResult, Type, type ModelProperty, type Program } from "@typespec/compiler"; | ||
import { Visibility } from "./metadata.js"; | ||
@@ -7,2 +7,4 @@ import { HeaderFieldOptions, PathParameterOptions, QueryParameterOptions } from "./types.js"; | ||
readonly property: ModelProperty; | ||
/** Path from the root of the operation parameters/returnType to the property. */ | ||
readonly path: (string | number)[]; | ||
} | ||
@@ -44,6 +46,2 @@ export interface HeaderProperty extends HttpPropertyBase { | ||
/** | ||
* Find the type of a property in a model | ||
*/ | ||
export declare function getHttpProperty(program: Program, property: ModelProperty, options?: GetHttpPropertyOptions): [HttpProperty, readonly Diagnostic[]]; | ||
/** | ||
* Walks the given input(request parameters or response) and return all the properties and where they should be included(header, query, path, body, as a body property, etc.) | ||
@@ -50,0 +48,0 @@ * |
import { compilerAssert, createDiagnosticCollector, walkPropertiesInherited, } from "@typespec/compiler"; | ||
import { Queue } from "@typespec/compiler/utils"; | ||
import { getHeaderFieldOptions, getPathParamOptions, getQueryParamOptions, isBody, isBodyRoot, isMultipartBodyProperty, isStatusCode, } from "./decorators.js"; | ||
@@ -9,6 +8,6 @@ import { createDiagnostic } from "./lib.js"; | ||
*/ | ||
export function getHttpProperty(program, property, options = {}) { | ||
function getHttpProperty(program, property, path, options = {}) { | ||
const diagnostics = []; | ||
function createResult(opts) { | ||
return [{ ...opts, property }, diagnostics]; | ||
return [{ ...opts, property, path }, diagnostics]; | ||
} | ||
@@ -33,6 +32,5 @@ const annotations = { | ||
}, | ||
property, | ||
}); | ||
} | ||
return [{ kind: "bodyProperty", property }, []]; | ||
return createResult({ kind: "bodyProperty" }); | ||
} | ||
@@ -48,27 +46,27 @@ else if (defined.length > 1) { | ||
if (annotations.header.name.toLowerCase() === "content-type") { | ||
return createResult({ kind: "contentType", property }); | ||
return createResult({ kind: "contentType" }); | ||
} | ||
else { | ||
return createResult({ kind: "header", options: annotations.header, property }); | ||
return createResult({ kind: "header", options: annotations.header }); | ||
} | ||
} | ||
else if (annotations.query) { | ||
return createResult({ kind: "query", options: annotations.query, property }); | ||
return createResult({ kind: "query", options: annotations.query }); | ||
} | ||
else if (annotations.path) { | ||
return createResult({ kind: "path", options: annotations.path, property }); | ||
return createResult({ kind: "path", options: annotations.path }); | ||
} | ||
else if (annotations.statusCode) { | ||
return createResult({ kind: "statusCode", property }); | ||
return createResult({ kind: "statusCode" }); | ||
} | ||
else if (annotations.body) { | ||
return createResult({ kind: "body", property }); | ||
return createResult({ kind: "body" }); | ||
} | ||
else if (annotations.bodyRoot) { | ||
return createResult({ kind: "bodyRoot", property }); | ||
return createResult({ kind: "bodyRoot" }); | ||
} | ||
else if (annotations.multipartBody) { | ||
return createResult({ kind: "multipartBody", property }); | ||
return createResult({ kind: "multipartBody" }); | ||
} | ||
compilerAssert(false, `Unexpected http property type`, property); | ||
compilerAssert(false, `Unexpected http property type`); | ||
} | ||
@@ -87,38 +85,41 @@ /** | ||
const visited = new Set(); | ||
const queue = new Queue([[type, undefined]]); | ||
while (!queue.isEmpty()) { | ||
const [model, rootOpt] = queue.dequeue(); | ||
function checkModel(model, path) { | ||
visited.add(model); | ||
let foundBody = false; | ||
let foundBodyProperty = false; | ||
for (const property of walkPropertiesInherited(model)) { | ||
const root = rootOpt ?? property; | ||
const propPath = [...path, property.name]; | ||
if (!isVisible(program, property, visibility)) { | ||
continue; | ||
} | ||
let httpProperty = diagnostics.pipe(getHttpProperty(program, property, options)); | ||
let httpProperty = diagnostics.pipe(getHttpProperty(program, property, propPath, options)); | ||
if (shouldTreatAsBodyProperty(httpProperty, visibility)) { | ||
httpProperty = { kind: "bodyProperty", property }; | ||
httpProperty = { kind: "bodyProperty", property, path: propPath }; | ||
} | ||
httpProperties.set(property, httpProperty); | ||
if (property !== root && | ||
(httpProperty.kind === "body" || | ||
httpProperty.kind === "bodyRoot" || | ||
httpProperty.kind === "multipartBody")) { | ||
const parent = httpProperties.get(root); | ||
if (parent?.kind === "bodyProperty") { | ||
httpProperties.delete(root); | ||
if (httpProperty.kind === "body" || | ||
httpProperty.kind === "bodyRoot" || | ||
httpProperty.kind === "multipartBody") { | ||
foundBody = true; | ||
} | ||
if (!(httpProperty.kind === "body" || httpProperty.kind === "multipartBody") && | ||
isModelWithProperties(property.type) && | ||
!visited.has(property.type)) { | ||
if (checkModel(property.type, propPath)) { | ||
foundBody = true; | ||
continue; | ||
} | ||
} | ||
if (httpProperty.kind === "body" || httpProperty.kind === "multipartBody") { | ||
continue; // We ignore any properties under `@body` or `@multipartBody` | ||
if (httpProperty.kind === "bodyProperty") { | ||
foundBodyProperty = true; | ||
} | ||
if (property.type.kind === "Model" && | ||
!type.indexer && | ||
type.properties.size > 0 && | ||
!visited.has(property.type)) { | ||
queue.enqueue([property.type, root]); | ||
} | ||
httpProperties.set(property, httpProperty); | ||
} | ||
return foundBody && !foundBodyProperty; | ||
} | ||
checkModel(type, []); | ||
return diagnostics.wrap([...httpProperties.values()]); | ||
} | ||
function isModelWithProperties(type) { | ||
return type.kind === "Model" && !type.indexer && type.properties.size > 0; | ||
} | ||
function shouldTreatAsBodyProperty(property, visibility) { | ||
@@ -125,0 +126,0 @@ if (visibility & Visibility.Read) { |
@@ -7,2 +7,3 @@ export { $lib } from "./lib.js"; | ||
export * from "./decorators.js"; | ||
export type { HttpProperty } from "./http-property.js"; | ||
export * from "./metadata.js"; | ||
@@ -9,0 +10,0 @@ export * from "./operations.js"; |
@@ -62,2 +62,3 @@ import { createDiagnosticCollector, } from "@typespec/compiler"; | ||
return diagnostics.wrap({ | ||
properties: metadata, | ||
parameters, | ||
@@ -64,0 +65,0 @@ verb, |
@@ -89,6 +89,7 @@ import { createDiagnosticCollector, getDoc, getErrorsDoc, getReturnsDoc, isErrorModel, isNullType, isVoidType, } from "@typespec/compiler"; | ||
headers, | ||
properties: metadata, | ||
}); | ||
} | ||
else { | ||
response.responses.push({ headers }); | ||
response.responses.push({ headers, properties: metadata }); | ||
} | ||
@@ -95,0 +96,0 @@ responses.set(statusCode, response); |
import { DiagnosticResult, Interface, ListOperationOptions, Model, ModelProperty, Namespace, Operation, Program, Tuple, Type } from "@typespec/compiler"; | ||
import { HeaderProperty } from "./http-property.js"; | ||
import { HeaderProperty, HttpProperty } from "./http-property.js"; | ||
/** | ||
@@ -251,2 +251,4 @@ * @deprecated use `HttpOperation`. To remove in November 2022 release. | ||
export interface HttpOperationParameters { | ||
/** Http properties */ | ||
readonly properties: HttpProperty[]; | ||
parameters: HttpOperationParameter[]; | ||
@@ -331,2 +333,4 @@ body?: HttpOperationBody | HttpOperationMultipartBody; | ||
export interface HttpOperationResponseContent { | ||
/** Http properties for this response */ | ||
readonly properties: HttpProperty[]; | ||
headers?: Record<string, ModelProperty>; | ||
@@ -333,0 +337,0 @@ body?: HttpOperationBody | HttpOperationMultipartBody; |
{ | ||
"name": "@typespec/http", | ||
"version": "0.59.0-dev.5", | ||
"version": "0.59.0-dev.6", | ||
"author": "Microsoft Corporation", | ||
@@ -5,0 +5,0 @@ "description": "TypeSpec HTTP protocol binding", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
322911
4147