Kong / Kong/insomnia

OpenAPI - $ref in an array is not parsed anymore

Open
#5,794 0 comments 0 reactions 0 assignees View on GitHub
B-bug PA-openapi-2-kong S-unverified
Dominant language
TypeScript
Stars
40k
Forks
2.4k
Avg merge
2d 13h
Merged PRs (30d)
61

Description

### Expected Behavior

The output of ``inso generate`` applied on openapi spec file with x-kong-plugin-request-validator enabled should contain $ref parsed for an array item in body_schema

```
_format_version: "1.1"
services:
- name: Example_spec
protocol: http
host: localhost
port: 80
path: /lohalhost
plugins:
- name: request-validator
config:
version: draft4
body_schema: "{}"
tags:
- OAS3_import
- OAS3file_apispec.yaml
enabled: true
routes:
- tags:
- OAS3_import
- OAS3file_apispec.yaml
name: Example_spec-some-path-id-post
methods:
- POST
paths:
- /some/path/(?[^\/]+)$
strip_path: false
plugins:
- name: request-validator
config:
version: draft4
parameter_schema:
- in: path
explode: false
required: true
name: id
schema: '{"type":"string"}'
style: simple
body_schema: '{"type":"array","items":{"title":"Item","type":"object","properties":{"createTime":{"type":"string","format":"date-time"},"type":{"type":"string"}}}}'
allowed_content_types:
- application/json
tags:
- OAS3_import
- OAS3file_apispec.yaml
enabled: true
tags:
- OAS3_import
- OAS3file_apispec.yaml
```

### Actual Behavior

The actual outcome is
```
_format_version: "1.1"
services:
- name: Example_spec
protocol: http
host: localhost
port: 80
path: /lohalhost
plugins:
- name: request-validator
config:
version: draft4
body_schema: "{}"
tags:
- OAS3_import
- OAS3file_apispec.yaml
enabled: true
routes:
- tags:
&a1
- OAS3_import
- OAS3file_apispec.yaml
name: Example_spec-some-path-id-post
methods:
- POST
paths:
- /some/path/(?[^/]+)$
strip_path: false
plugins:
- name: request-validator
config:
version: draft4
parameter_schema:
- in: path
explode: false
required: true
name: id
schema: '{"type":"string"}'
style: simple
body_schema: '{"type":"array","items":{"$ref":"#/components/schemas/Item"}}'
allowed_content_types:
- application/json
tags:
- OAS3_import
- OAS3file_apispec.yaml
enabled: true
tags: *a1
```

### Reproduction Steps

``inso generate config apispec.yaml``

apispec.yaml content
```
openapi: 3.0.3
info:
version: 0.0.1
title: Example spec
servers:
- url: lohalhost
paths:
"/some/path/{id}":
post:
parameters:
- name: id
in: path
required: true
style: simple
schema:
type: string
requestBody:
content:
application/json:
schema:
type: array
items:
"$ref": "#/components/schemas/Item"
responses:
'200':
description: OK
'201':
description: Created
'401':
description: Unauthorized
'403':
description: Forbidden
'404':
description: Not Found
components:
schemas:
Item:
title: Item
type: object
properties:
createTime:
type: string
format: date-time
type:
type: string
x-kong-plugin-request-validator:
enabled: true
```

### Is there an existing issue for this?

- [X] I have searched the [issue tracker](https://www.github.com/Kong/insomnia/issues) for this problem.

### Additional Information

Worked well till v3.4.0. Was able to narrow it down to this particular change #4911

[packages/openapi-2-kong/src/declarative-config/plugins.ts](https://github.com/Kong/insomnia/pull/4911/files#diff-7983488bf791acc4edfa9854fff05d15154c72497a57e921667ecfbb5f0a036f)
```
if (Array.isArray(source)) {
// if the source is an array literal, we want to just return it literally (i.e. skip) because all we're looking for is the `$ref` value (which is always an object in the OpenAPI spec). For the purposes of this function, we don't care about arrays because it means the value isn't relevant to what this function tries to do (which is, resolving `$ref`s).
return componentsRefMap;
```
This would be my guess

### Insomnia Version

cli 3.12.0

### What operating system are you using?

Ubuntu

### Operating System Version

wsl for windows

### Installation method

downloaded cli for linux from github

### Last Known Working Insomnia version

cli 3.3.0

Contributor guide

Open the contributing guide

Research direction

Start in packages/openapi-2-kong/src/declarative-config/plugins.ts and compare the array handling introduced by PR #4911. Run `inso generate config apispec.yaml` with the reproduction spec and verify that the array item's `$ref` resolves to the Item schema in body_schema and that the generated YAML no longer contains the incorrect anchor output.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.