Repository: Redocly/redocly-cli (ts)

Issue:
Callbacks typing seems wrong
**Describe the bug**

The NamedCallbacks and Callback types seem wrong.

```js
  NamedCallbacks: mapOf('PathItem'),
``` 
Note: Seems like it should be a `mapOf('Callback')`.


And

```js
const Callback = {
  properties: {},
  additionalProperties: 'PathItem',
};
```

This seems incorrect. The callback is a map of expression to `PathItem`. It seems it should be more similar to:

```js
  Callback: mapOf('PathItem'),
```


**To Reproduce**
Steps to reproduce the behavior (minimal definition):

```yaml
openapi: 3.1.0
info:
  title: Callback test
  version: 'alpha'
components:
  callbacks:
    resultCallback:
      '{$url}':
        post:
          requestBody:
            description: Callback payload
            content:
              'application/json':
                schema:
                  type: object
                  properties:
                    test:
                      type: string
          responses:
            '200':
              description: callback successfully processed

```

Run `openapi lint`.

```
Property `{$url}` is not expected here.

 6 | callbacks:
 7 |   resultCallback:
 8 |     '{$url}':
 9 |       post:
10 |         requestBody:

Error was generated by the spec rule.
```

**Expected behavior**
I expected that the callback is correct. If not, what is wrong?


**Logs**

```
Property `{$url}` is not expected here.

 6 | callbacks:
 7 |   resultCallback:
 8 |     '{$url}':
 9 |       post:
10 |         requestBody:

Error was generated by the spec rule.
```

**`openapi-cli` Version(s)**

1.0.0-beta.94


**`Node.js` Version(s)**

16

**Additional context**

None

Pull request:
fix: update callback types
## What/Why/How?
Fix the NamedCallbacks and Callback types.

## Reference
Fix #664

## Testing

## Screenshots (optional)

## Check yourself

- [x] Code is linted
- [ ] Tested with redoc/reference-docs/workflows
- [x] All new/updated code is covered with tests

## Security

- [x] Security impact of change has been considered
- [x] Code follows company security practices and guidelines


Patch:
diff --git a/docs/resources/rules/assertions.md b/docs/resources/rules/assertions.md
index 5751b3db..31bea13b 100644
--- a/docs/resources/rules/assertions.md
+++ b/docs/resources/rules/assertions.md
@@ -470,7 +470,7 @@ List of types for OpenAPI 3.0 and 3.1:
 - NamedHeaders: mapOf('Header')
 - NamedSecuritySchemes: mapOf('SecurityScheme')
 - NamedLinks: mapOf('Link')
-- NamedCallbacks: mapOf('PathItem')
+- NamedCallbacks: mapOf('Callback')
 - ImplicitFlow
 - PasswordFlow
 - ClientCredentials
diff --git a/packages/core/src/types/oas3.ts b/packages/core/src/types/oas3.ts
index b396d62a..4d73b6c4 100755
--- a/packages/core/src/types/oas3.ts
+++ b/packages/core/src/types/oas3.ts
@@ -139,11 +139,6 @@ const Parameter: NodeType = {
   requiredOneOf: ['schema', 'content'],
 };
 
-const Callback = {
-  properties: {},
-  additionalProperties: 'PathItem',
-};
-
 const Operation: NodeType = {
   properties: {
     tags: {
@@ -479,7 +474,7 @@ export const Oas3Types: Record<string, NodeType> = {
   PathItem,
   Parameter,
   Operation,
-  Callback,
+  Callback: mapOf('PathItem'),
   RequestBody,
   MediaTypeMap,
   MediaType,
@@ -503,7 +498,7 @@ export const Oas3Types: Record<string, NodeType> = {
   NamedHeaders: mapOf('Header'),
   NamedSecuritySchemes: mapOf('SecurityScheme'),
   NamedLinks: mapOf('Link'),
-  NamedCallbacks: mapOf('PathItem'),
+  NamedCallbacks: mapOf('Callback'),
   ImplicitFlow,
   PasswordFlow,
   ClientCredentials,


Test patch:
diff --git a/packages/core/src/__tests__/lint.test.ts b/packages/core/src/__tests__/lint.test.ts
index 5fb646d9..716ba4da 100644
--- a/packages/core/src/__tests__/lint.test.ts
+++ b/packages/core/src/__tests__/lint.test.ts
@@ -192,4 +192,41 @@ describe('lint', () => {
       `Invalid OpenAPI version: should be a string but got "number"`,
     );
   });
+
+  it("spec rule shouldn't throw an error for named callback", async () => {
+    const document = parseYamlToDocument(
+      outdent`
+      openapi: 3.1.0
+      info:
+        title: Callback test
+        version: 'alpha'
+      components:
+        callbacks:
+          resultCallback:
+            '{$url}':
+              post:
+                requestBody:
+                  description: Callback payload
+                  content:
+                    'application/json':
+                      schema:
+                        type: object
+                        properties:
+                          test:
+                            type: string
+                responses:
+                  '200':
+                    description: callback successfully processed
+    `,
+      'foobar.yaml',
+    );
+
+    const results = await lintDocument({
+      externalRefResolver: new BaseResolver(),
+      document,
+      config: await makeConfig({ spec: 'error' }),
+    });
+
+    expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`);
+  });
 });