Skip to content
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

fix: soft fail fallback response description lookups #832

Merged

Conversation

samchungy
Copy link
Contributor

@samchungy samchungy commented Oct 26, 2024

I tried fixing this issue in my library as raised in samchungy/fastify-zod-openapi#172

My library dynamically generates $refs for users so they don't need to register anything upfront and plops them into the components section at the end.

As an example, my users will fill in the following:

responses: {
  200: z.string().openapi({ ref: 'Foo' })
}

The transform hook will output:

responses: {
  200: {
    $ref: '#/components/schemas/Foo'
  }
}

I tried dynamically injecting the components in the transform hook and even when I tried adding components into the fastifySwagger constructor this seems to not work.

fastify.register(fastifySwagger, {
    openapi: {
      components: {
		schema: {
		  foo: {
		    type: 'string'
		  }
		}
	}
});

This function does not seem to reference openapi components:

function resolveSchemaReference (rawSchema, ref) {
const resolvedReference = ref.resolve(rawSchema, { externalSchemas: [ref.definitions().definitions] })
// Ref has format `#/definitions/id`
const schemaId = resolvedReference?.$ref?.split('/', 3)[2]
if (schemaId === undefined) {
return undefined
}
return resolvedReference.definitions[schemaId]
}
module.exports = {
resolveSchemaReference
}
and the rest of the code's support for components seem fairly limited.

Additionally this line in particular:

const schemaId = resolvedReference?.$ref?.split('/', 3)[2]

only returns schemas.

I might revisit this repo to add proper openapi component support and start incrementally adding components to the openapiObject construct but for now I want to unblock my users.

Checklist

@samchungy samchungy force-pushed the fix-custom-transform-ref-resolutions branch from 463f679 to 3d2acfd Compare October 26, 2024 07:45
@samchungy samchungy changed the title Address schema lookup failing fix: soft fail fallback response description lookups Oct 26, 2024
@samchungy samchungy marked this pull request as ready for review October 26, 2024 07:48
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@samchungy
Copy link
Contributor Author

Hey @gurgunday would you mind merging and releasing a patch version for this package?

I would love to cut a new release of my package, pinning the safe version for my consumers. Please and thank you! 🙏

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit e35d2f6 into fastify:master Oct 30, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants