-
-
Notifications
You must be signed in to change notification settings - Fork 210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
$refs in arrays don't work #612
Comments
@climba03003 wdyt? |
I think it would be something related to |
Having exactly the same issue 🥲 |
Had this issue for over a day, thought I was doing something wrong. Eventually had to dereference with https://github.com/APIDevTools/json-schema-ref-parser |
Hey, works for me. But I need to give the $ref as the exact same string as the $id was. fastify.addSchema({
$id: 'User',
type: 'object',
description: 'User basic data',
properties: UserSchema,
required: omitOptional(Object.keys(UserSchema)),
});
const AcceptedInvitationsSchema = {
...
users: { type: 'array', $ref: 'User' }
} |
Since components:
schemas:
def-0:
type: object
required:
- name
properties:
name:
type: string
title: /components/schemas/Pet
def-1:
type: array
maxItems: 100
items:
# "Pet" component is defined as "#/components/def-0/Pet" above
# OpenAPI will try to resolve at "#/components/schemas/Pet", instead
$ref: "#/components/schemas/Pet"
title: /components/schemas/Pets In order to let OpenAPI resolve /**
* This is needed since Fastify by default names components as "def-${i}"
* https://github.com/fastify/fastify-swagger?tab=readme-ov-file#managing-your-refs
*/
refResolver: {
buildLocalReference: (json, baseUri, fragment, i) => {
const OPEN_API_COMPONENTS_SCHEMAS_PATH = '/components/schemas/';
if (
typeof json.$id === 'string' &&
json.$id.startsWith(OPEN_API_COMPONENTS_SCHEMAS_PATH)
) {
const name = json.$id.replace(OPEN_API_COMPONENTS_SCHEMAS_PATH, '');
if (name) {
return name;
}
}
// @TODO Support naming component schemas different than "components.schema"
return `def-${i}`;
},
}, The implementation should be adjusted based on the actual registered schemas. |
Prerequisites
Fastify version
3.29.0
Plugin version
6.1.0
Node.js version
v16.13.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
12.3.1
Description
Hello everyone,
I was trying to reference my array items to an existing definition in the same schema when I got this error:
Could not resolve reference: Could not resolve pointer: /definitions/<name>
However, the reference does exist, as it works when I reference it as an object instead of an array.
I have the following (artificial) scenario: I have an object in the following format:
I want to be able to expose this as a single object and in an array. If I reference to that, only the object one works.
If you look at the below image, you see that SingleId works exactly as it's supposed to, while ListOfIds only shows "string". In addition, you see the error on top.
See the steps to reproduce below.
Steps to Reproduce
Expected Behavior
I would expect that I can reference to model in an array (by doing so as mentioned above - putting a
$ref
initems
).The text was updated successfully, but these errors were encountered: