samchon / typia

Super-fast/easy runtime validators and serializers via transformation
https://typia.io/
MIT License
4.45k stars 153 forks source link

$id not generated when using typia.application #461

Closed GiovanniCardamone closed 1 year ago

GiovanniCardamone commented 1 year ago

Bug Report

Summary

Hello, i'm using typia to generate dynamically from interfaces, my Definitions.

I have a file where i generate my definitions in this way:

export default typia.application<
    [Deck, User, Game],
    "swagger"
>()

and also this definition is passed to the swagger definition file.

now i want to reference my models using $id

https://json-schema.org/understanding-json-schema/basics.html#declaring-a-unique-identifier

but the output of the typia.application does not contains the $id for each schema

{
  schemes: {
    schemas: [
      { '$ref': '#/components/schemas/Deck' },
      { '$ref': '#/components/schemas/User' },
      { '$ref': '#/components/schemas/Game' }
    ],
    components: {
      schemas: {
        Deck: {
          type: 'object',
          properties: {
            name: {
              type: 'string',
              nullable: false,
              'x-typia-required': true
            },
            values: {
              type: 'array',
              items: {
                type: 'string',
                nullable: false,
                'x-typia-required': true
              },
              nullable: false,
              'x-typia-required': true
            }
          },
          nullable: false,
          required: [ 'name', 'values' ],
          'x-typia_jsDocTags': []
        },
        User: {
          type: 'object',
          properties: {
            id: {
              type: 'string',
              nullable: false,
              'x-typia-required': true
            },
            name: {
              type: 'string',
              nullable: false,
              'x-typia-required': true
            }
          },
          nullable: false,
          required: [ 'id', 'name' ],
          'x-typia_jsDocTags': []
        },
        Game: {
          type: 'object',
          properties: {
            participants: {
              type: 'array',
              items: {
                '$ref': '#/components/schemas/User',
                'x-typia-required': true
              },
              nullable: false,
              'x-typia-required': true
            },
            deck: {
              '$ref': '#/components/schemas/Deck',
              'x-typia-required': true
            }
          },
          nullable: false,
          required: [ 'participants', 'deck' ],
          'x-typia_jsDocTags': []
        }
      }
    },
    purpose: 'swagger',
    prefix: '#/components/schemas'
  }
}

that's bad because doesn't allow me to reference my schema directly using something like:

definitions.components.schemas.Deck.$id // undefined

also where IJsonComponents it's defined i can see there is a $id listed.

export interface IJsonComponents {
    schemas: Record<string, IJsonComponents.IObject>;
}
export declare namespace IJsonComponents {
    interface IObject {
        $id?: string;
        type: "object";
        nullable: boolean;
        properties: Record<string, IJsonSchema>;
        patternProperties?: Record<string, IJsonSchema>;
        additionalProperties?: IJsonSchema;
        required?: string[];
        description?: string;
        "x-typia_jsDocTags"?: IJsDocTagInfo[];
        $recursiveAnchor?: boolean;
    }
}

is this planned to add or i should put manually the reference each time i want to use a schema?

Thankyou

GiovanniCardamone commented 1 year ago

on file: src/programmers/internal/application_object.ts:81 there is this snippet:

$id:
                options.purpose === "ajv"
                    ? options.prefix + "/" + key
                    : undefined,

as i can see it's only generated for "ajw", there is a reason why it's this way?

samchon commented 1 year ago

The $id property does not supported in swagger.

https://swagger.io/docs/specification/data-models/keywords/

image

If try to add the $id property in any object definition, an error would be occured like below:

image

https://editor.swagger.io/?url=https%3A%2F%2Fraw.githubusercontent.com%2Fsamchon%2Ffake-toss-payments-server%2Fmaster%2Fdist%2Fswagger.json