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

Exact JSON schema downgrading. #52

Merged
merged 1 commit into from
Sep 24, 2024
Merged

Exact JSON schema downgrading. #52

merged 1 commit into from
Sep 24, 2024

Conversation

samchon
Copy link
Owner

@samchon samchon commented Sep 24, 2024

Found some JSON schema downgrade bugs.

Nested oneOf in the oneOf type

When oneOf type is referencing another oneOf type that is caplused in the components.schema as named schema, JSON schema downgraders had defined nested oneOf type under an oneOf type.

This PR fixes the bug, so that below test code works from now on.

export const test_llm_schema_union = (): void => {
  const components: OpenApi.IComponents = {
    schemas: {
      named: {
        oneOf: [
          {
            const: 4,
          },
          {
            const: 5,
          },
        ],
      },
    },
  };
  const schema: OpenApi.IJsonSchema = {
    oneOf: [
      {
        const: 3,
      },
      {
        $ref: "#/components/schemas/named",
      },
    ],
  };

  const llm: ILlmSchema | null = HttpLlm.schema({
    components,
    schema,
  });
  TestValidator.equals("union")(llm)({
    type: "number",
    enum: [3, 4, 5],
  });
};

Double nested $ref type is nullable case

When nullable type is nested in the $ref type, JSON schema downgrader could not detect it properly. This PR fixes the bug, so that makes below test function to be worked properly.

export const test_json_schema_downgrade_v30_nullable = (): void => {
  const original: OpenApi.IComponents = {
    schemas: {
      union: {
        oneOf: [
          {
            type: "null",
          },
          {
            type: "string",
          },
          {
            type: "number",
          },
        ],
      },
    },
  };
  const components: OpenApiV3.IComponents = {
    schemas: {},
  };
  const schema: OpenApiV3.IJsonSchema = OpenApiV3Downgrader.downgradeSchema({
    original,
    downgraded: components,
  })({
    oneOf: [
      {
        type: "boolean",
      },
      {
        $ref: "#/components/schemas/union",
      },
    ],
  } satisfies OpenApi.IJsonSchema);
  TestValidator.equals("nullable")({
    components,
    schema,
  })({
    components: {
      schemas: {
        "union.Nullable": {
          oneOf: [
            {
              type: "string",
              nullable: true,
            },
            {
              type: "number",
              nullable: true,
            },
          ],
        },
      },
    },
    schema: {
      oneOf: [
        {
          type: "boolean",
          nullable: true,
        },
        {
          $ref: "#/components/schemas/union.Nullable",
        },
      ],
    },
  });
};

Found some JSON schema downgrade bugs.

## Nested `oneOf` in the `oneOf` type
When oneOf type is referencing another oneOf type that is caplused in the `components.schema` as named schema, JSON schema downgraders had defined nested `oneOf` type under an `oneOf` type.

This PR fixes the bug, so that below test code works from now on.

```typescript
export const test_llm_schema_union = (): void => {
  const components: OpenApi.IComponents = {
    schemas: {
      named: {
        oneOf: [
          {
            const: 4,
          },
          {
            const: 5,
          },
        ],
      },
    },
  };
  const schema: OpenApi.IJsonSchema = {
    oneOf: [
      {
        const: 3,
      },
      {
        $ref: "#/components/schemas/named",
      },
    ],
  };

  const llm: ILlmSchema | null = HttpLlm.schema({
    components,
    schema,
  });
  TestValidator.equals("union")(llm)({
    type: "number",
    enum: [3, 4, 5],
  });
};
```

## Double nested `$ref` type is `nullable` case
When `nullable` type is nested in the `$ref` type, JSON schema downgrader could not detect it properly. This PR fixes the bug, so that makes below test function to be worked properly.

```typescript
export const test_json_schema_downgrade_v30_nullable = (): void => {
  const original: OpenApi.IComponents = {
    schemas: {
      union: {
        oneOf: [
          {
            type: "null",
          },
          {
            type: "string",
          },
          {
            type: "number",
          },
        ],
      },
    },
  };
  const components: OpenApiV3.IComponents = {
    schemas: {},
  };
  const schema: OpenApiV3.IJsonSchema = OpenApiV3Downgrader.downgradeSchema({
    original,
    downgraded: components,
  })({
    oneOf: [
      {
        type: "boolean",
      },
      {
        $ref: "#/components/schemas/union",
      },
    ],
  } satisfies OpenApi.IJsonSchema);
  TestValidator.equals("nullable")({
    components,
    schema,
  })({
    components: {
      schemas: {
        "union.Nullable": {
          oneOf: [
            {
              type: "string",
              nullable: true,
            },
            {
              type: "number",
              nullable: true,
            },
          ],
        },
      },
    },
    schema: {
      oneOf: [
        {
          type: "boolean",
          nullable: true,
        },
        {
          $ref: "#/components/schemas/union.Nullable",
        },
      ],
    },
  });
};
```
@samchon samchon added bug Something isn't working enhancement New feature or request labels Sep 24, 2024
@samchon samchon self-assigned this Sep 24, 2024
Copy link
Owner Author

@samchon samchon left a comment

Choose a reason for hiding this comment

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

OK, no problem may be.

@samchon samchon merged commit 44a753a into master Sep 24, 2024
4 checks passed
@samchon samchon deleted the feature/schema branch September 24, 2024 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant