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

Registered parameter can not have different name property or point to a schema with a different name #284

Open
brupxxxlgroup opened this issue Dec 12, 2024 · 0 comments

Comments

@brupxxxlgroup
Copy link

brupxxxlgroup commented Dec 12, 2024

I have encountered the following problems.

Consider the following script

import {
  extendZodWithOpenApi,
  OpenApiGeneratorV3,
  OpenAPIRegistry,
} from "@asteasolutions/zod-to-openapi";
import YAML from "yaml";
import { z } from "zod";

extendZodWithOpenApi(z);

try {
  const registry = new OpenAPIRegistry();

  const regParam = registry.registerParameter(
    "RegParam",
    z.string().openapi("OtherName", {
      param: {
        in: "path",
        name: "RegParam",
      },
    })
  );

  registry.registerPath({
    method: "get",
    path: "/foo",
    request: {
      params: z.object({
        RegParam: regParam,
      }),
    },
    responses: {},
  });

  const generatedApiJson = new OpenApiGeneratorV3(
    registry.definitions
  ).generateDocument({
    openapi: "3.0.0",
    info: { title: "App API", version: "1.0.0" },
  });

  console.log("Generated API JSON");
  console.log(YAML.stringify(generatedApiJson));
} catch (e) {
  console.dir(e);
}

Tihs will generate following yaml

openapi: 3.0.0
info:
  title: App API
  version: 1.0.0
components:
  schemas:
    RegParam:
      type: string
  parameters:
    RegParam:
      schema:
        $ref: "#/components/schemas/RegParam"
      required: true
      in: path
      name: RegParam
paths:
  /foo:
    get:
      parameters:
        - $ref: "#/components/parameters/RegParam"
      responses: {}

But i would have expected following outcome

openapi: 3.0.0
info:
  title: App API
  version: 1.0.0
components:
  schemas:
    OtherName:
      type: string
  parameters:
    RegParam:
      schema:
        $ref: "#/components/schemas/OtherName"
      required: true
      in: path
      name: RegParam
paths:
  /foo:
    get:
      parameters:
        - $ref: "#/components/parameters/RegParam"
      responses: {}
  1. So the first problem is that registerParameter ignores the case if a schemas already has a refid and just overwrites the schemas refid with the name of the parameter. I want to have a registered parameter where i can point to a schema with a different name then the parameter itself.

  2. It seems that a registered paramater can not have a different name property then the name of the parameter itself. When you do this.

  const regParam = registry.registerParameter(
    "RegParam",
    z.string().openapi("OtherName", {
      param: {
        in: "path",
        name: "MyOtherName",
      },
    })
  );

This will end up with an exception ConflictError {message: 'Conflicting names for parameter', data: {…}}

I would expect something like this

openapi: 3.0.0
info:
  title: App API
  version: 1.0.0
components:
  schemas:
    OtherName:
      type: string
  parameters:
    RegParam:
      schema:
        $ref: "#/components/schemas/OtherName"
      required: true
      in: path
      name: MyOtherName
paths:
  /foo:
    get:
      parameters:
        - $ref: "#/components/parameters/RegParam"
      responses: {}
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

No branches or pull requests

1 participant