# OasOperation

CLASS in `@skmtc/core 0.63.4`

An operation of the document: one method on one path, with its parameters,
request body and responses. It is the subject of an operation generator.

- `parameters` has only the operation's own parameters. `toParams`
  adds those of its path item and resolves the references.
- When `security` is `undefined`, the security of the document applies. An
  empty array means that the operation needs none.

## Signature

```ts
import { OasOperation } from '@skmtc/core'

class OasOperation extends OasBase
```

## Constructor

### constructor

```ts
constructor(fields: OperationFields, context?: ParseContextType)
```

Creates a new OasOperation instance from operation field data.

- `fields`: Operation field data from OpenAPI specification

## Properties

- `oasType` ('operation'): Type identifier for OAS operation objects
- `path` (string): The API path for this operation
- `method` (Method): The HTTP method for this operation
- `pathItem` (OasPathItem | undefined): The parent path item containing this operation
- `operationId` (string | undefined): Unique identifier for the operation
- `summary` (string | undefined): Brief summary of the operation
- `tags` (string[] | undefined): Tags for organizing operations in documentation
- `description` (string | undefined): Detailed description of the operation
- `parameters` ((OasParameter | OasRef<'parameter'>)[] | undefined): Parameters accepted by this operation
- `requestBody` (OasRequestBody | OasRef<'requestBody'> | undefined): Request body specification for this operation
- `responses` (Record<string, OasResponse | OasRef<'response'>>): Response specifications mapped by status code
- `security` (OasSecurityRequirement[] | undefined): Security requirements for this operation
- `deprecated` (boolean | undefined): Whether this operation is deprecated
- `externalDocs` (OasExternalDocs | undefined): External documentation for this operation
- `extensionFields` (Record<string, unknown> | undefined): OpenAPI specification extensions
- `servers` (OasServer[] | undefined): Servers for this operation

## Methods

### toSuccessResponse

```ts
toSuccessResponse(): OasResponse | OasRef<'response'> | undefined
```

Returns the successful response definition for this operation.

Looks for the lowest numbered 2xx response code and returns its response definition.

Returns: Success response object or undefined if none found

### toSuccessResponseCode

```ts
toSuccessResponseCode(): string | undefined
```

Returns the HTTP status code for the primary success response.

Finds the lowest numbered 2xx status code in the responses.

Returns: Success status code as string or undefined if none found

### toRequestBody

```ts
toRequestBody<V>(
  map: (args: ToRequestBodyMapArgs) => V,
  mediaType?: string
): V | undefined
```

Maps the request body schema to a custom value using the provided mapping function.

- `map`: Function to transform the request body schema and metadata
- `mediaType`: Media type to extract schema from, an exact lookup. Omitted: the media type `OasRequestBody.toJsonContent` picks.

Returns: Mapped value or undefined if no request body schema found

### traverse

```ts
traverse(path: SchemaPath, mediaType?: string): OasSchema | OasRef<'schema'>
```

Navigate into this operation's response or request body schema along an
`SchemaPath`. The first segment selects the entry point - `'SuccessResponse'` (the 2xx response body schema) or `'RequestBody'` (the
request body schema) - and the rest descend through object properties,
array `items`, and union members, resolving `$ref`s on the way. See
`traverseSchema`.

- `path`: Schema path; `path[0]` must be `'SuccessResponse'` or `'RequestBody'`.
- `mediaType`: Media type the body schema is read from, an exact lookup. Omitted: the JSON media type (`application/json`, else the first JSON-like type, else `*\/*`).

Returns: The schema at the path (may be an unresolved `$ref`).

Throws:

- `Error`: Error on an unknown root, a missing response/request body, or a non-navigable segment.

### toEffectiveParameters

```ts
toEffectiveParameters(): (OasParameter | OasRef<'parameter'>)[]
```

The parameters that apply to this operation, as OpenAPI defines them: the
path item's parameters apply to each of its operations, and an operation's
own parameter with the same name and location replaces the path item's.
The path item's come first.

References stay references, so a caller can resolve each one on its own.
A reference that names nothing (kept when `parse.danglingRefs` lists
`parameter`) keeps its place, and resolving it throws `DanglingRefError`.
`OasOperation.toParams` resolves them all.

Returns: the parameters, path item's first

### toParams

```ts
toParams(filter?: OasParameterLocation[]): OasParameter[]
```

Resolve all parameters and optionally filter by location: the resolved
`OasOperation.toEffectiveParameters`.

- `filter`: only include parameters from specified locations

Throws:

- `DanglingRefError`: when a parameter is a reference that names nothing

### toParametersObject

```ts
toParametersObject(filter?: OasParameterLocation[]): OasObject
```

Creates an OAS object representation of operation parameters.

- `filter`: Optional array of parameter locations to include

Returns: OAS object with parameter properties

### toJsonSchema

```ts
toJsonSchema(options: ToJsonSchemaOptions): OpenAPIV3.OperationObject
```

Converts the operation to OpenAPI v3 JSON schema format.

- `options`: Conversion options for nested components

Returns: OpenAPI v3 operation object

Throws:

- `DanglingRefError`: when a parameter is a reference that names nothing, as `OasOperation.toParams` does: there is no parameter to write, and the caller decides what to do

### toJSON

```ts
toJSON(): object
```

Serializes the operation to a plain JavaScript object.

Returns: Plain object representation of the operation
