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

Support Components Section in OpenAPI Standard #2920

Open
1 task done
AkarinServer opened this issue Dec 4, 2024 · 5 comments · May be fixed by #2921
Open
1 task done

Support Components Section in OpenAPI Standard #2920

AkarinServer opened this issue Dec 4, 2024 · 5 comments · May be fixed by #2921

Comments

@AkarinServer
Copy link

Describe the feature

According to the OpenAPI Standard, OpenAPI should support Components function.

Before using Components:

paths:
  /users/{userId}:
    get:
      summary: Get a user by ID
      parameters: ...
      responses:
        "200":
          description: A single user.
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: integer
                  name:
                    type: string
  /users:
    get:
      summary: Get all users
      responses:
        "200":
          description: A list of users.
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    id:
                      type: integer
                    name:
                      type: string

After using components:

paths:
  /users/{userId}:
    get:
      summary: Get a user by ID
      parameters: ...
      responses:
        "200":
          description: A single user.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/User"
  /users:
    get:
      summary: Get all users
      responses:
        "200":
          description: A list of users.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/User"

components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string

Hopefully can support this function soon.

Additional information

  • Would you be willing to help implement this feature?
@OskarLebuda
Copy link

@AkarinServer what do you think about something like that?

defineRouteMeta({
  openAPI: {
    responses: {
      200: { description: 'OK ', content: { 'application/json': { schema: { $ref: '#/components/User' } } } },
    },
    components: {
      User: {
        type: 'object',
        properties: {
          id: { type: 'integer' },
          name: { type: 'string' },
        },
      }
    },
  },
});

export default eventHandler(async (event) => {
  return { hello: 'world!' };
});

It's a POC of components of the API. Let me know if it's something you're looking for.
Of course the User will be available later as a component so you will just use it like:

defineRouteMeta({
  openAPI: {
    responses: {
      200: { description: 'OK ', content: { 'application/json': { schema: { $ref: '#/components/User' } } } },
    },
  },
});

export default eventHandler(async (event) => {
  return { hello: 'world 2!' };
});

@OskarLebuda OskarLebuda linked a pull request Dec 5, 2024 that will close this issue
8 tasks
@AkarinServer
Copy link
Author

Unbelievable! That's exactly what I'm describing! I'm so impressed for your support speed!!! Thanks for your awesome job!

@OskarLebuda
Copy link

Unbelievable! That's exactly what I'm describing! I'm so impressed for your support speed!!! Thanks for your awesome job!

Now we have to wait for the CR and PR approve 😄

@laplaceliu
Copy link

laplaceliu commented Dec 31, 2024

@AkarinServer what do you think about something like that?

defineRouteMeta({
  openAPI: {
    responses: {
      200: { description: 'OK ', content: { 'application/json': { schema: { $ref: '#/components/User' } } } },
    },
    components: {
      User: {
        type: 'object',
        properties: {
          id: { type: 'integer' },
          name: { type: 'string' },
        },
      }
    },
  },
});

export default eventHandler(async (event) => {
  return { hello: 'world!' };
});

It's a POC of components of the API. Let me know if it's something you're looking for. Of course the User will be available later as a component so you will just use it like:

defineRouteMeta({
  openAPI: {
    responses: {
      200: { description: 'OK ', content: { 'application/json': { schema: { $ref: '#/components/User' } } } },
    },
  },
});

export default eventHandler(async (event) => {
  return { hello: 'world 2!' };
});

I don't think this is a reasonable approach.

In fact, defineRouteMeta accepts a parameter of type NitroRouteMeta.

interface NitroRouteMeta {
    openAPI?: OperationObject;
}

The type of openAPI is OperationObject:

export interface OperationObject extends Extensable {
    tags?: string[];
    summary?: string;
    description?: string;
    externalDocs?: ExternalDocumentationObject;
    operationId?: string;
    parameters?: (ParameterObject | ReferenceObject)[];
    requestBody?: RequestBodyObject | ReferenceObject;
    responses?: ResponsesObject;
    callbacks?: Record<string, CallbackObject | ReferenceObject>;
    deprecated?: boolean;
    security?: SecurityRequirementObject[];
    servers?: ServerObject[];
}

As you can see, there is no field called components in OperationObject.

I think it is still necessary to find an openapi generator to see how components is implemented.

@OskarLebuda
Copy link

@laplaceliu look at that PR 💻
#2921

The NitroRouteMeta is extended

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants