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

Problem with arrays #27

Closed
gempain opened this issue Jan 22, 2020 · 3 comments
Closed

Problem with arrays #27

gempain opened this issue Jan 22, 2020 · 3 comments

Comments

@gempain
Copy link

gempain commented Jan 22, 2020

Hi, thanks a lot for this great package !

I've got one issue with arrays: I always get type $ref: "#/definitions/Array". I can reproduce with the following test (just add it and run jest ./__tests__/arrays.test.ts):

import 'reflect-metadata';
import {Type} from 'class-transformer';
import {getFromContainer, IsArray, MetadataStorage, ValidateNested, ValidationTypes} from 'class-validator';
import {validationMetadatasToSchemas} from '../src';
import {defaultConverters} from '../src/defaultConverters';

describe('arrays', () => {

    class Child {
        name: string;
    }

    class Parent {
        @ValidateNested({each: true})
        @IsArray()
        @Type(() => Child)
        children: Child[];
        @ValidateNested()
        @Type(() => Child)
        child: Child;
    }

    it('should define $ref to Child', () => {
        console.log(Parent, Child);
        const metadatas = (getFromContainer(MetadataStorage) as any).validationMetadatas;
        const schemas = validationMetadatasToSchemas(metadatas, {
            additionalConverters: {
                [ValidationTypes.IS_ARRAY]: (meta, _options) => {
                  console.log(meta);
                  return defaultConverters[ValidationTypes.IS_ARRAY];
                },
            }
        });
        console.log(JSON.stringify(schemas, null, 4));
    });

});

Which prints:

{
  "Parent": {
    "properties": {
      "children": {
        "items": {
          "$ref": "#/definitions/Array"
        },
        "type": "array"
      },
      "child": {
        "$ref": "#/definitions/Child"
      }
    },
    "type": "object",
    "required": [
      "children",
      "child"
    ]
  }
}

Any clue ?

@gempain gempain closed this as completed Jan 22, 2020
@gempain
Copy link
Author

gempain commented Jan 22, 2020

Nevermind, just missed it in the docs:

{
    classTransformerMetadataStorage: defaultMetadataStorage,
}

@feimosi
Copy link

feimosi commented Nov 6, 2020

I'm having a similar issue. Even though I set up metadata storage it still returns "$ref": "#/definitions/Child" instead of "$ref": "#/components/schemas/Child".

Here's the code:

import { validationMetadatasToSchemas } from 'class-validator-jsonschema';
import { defaultMetadataStorage } from 'class-transformer/storage';

const schemas = validationMetadatasToSchemas({
  classTransformerMetadataStorage: defaultMetadataStorage,
});
import { ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';

class Child {
  name: string;
}

class BadRequestValidationError {
  @ValidateNested({each: true})
  @Type(() => Child)
  children!: Child[];
  @ValidateNested()
  @Type(() => Child)
  child!: Child;
}

The above definitions generated the following BadRequestValidationError schema:

{
  "properties": {
    "children": {
      "items": {
        "$ref": "#/definitions/Child"
      },
      "type": "array"
    },
    "child": {
      "$ref": "#/definitions/Child"
    }
  },
  "type": "object",
  "required": [
    "children",
    "child"
  ]
}

Logging defaultMetadataStorage content gives me the following output:

MetadataStorage {
  _typeMetadatas: Map(1) {
    [Function: BadRequestValidationError] => Map(2) { 'children' => [TypeMetadata], 'child' => [TypeMetadata] }
  },
  _transformMetadatas: Map(0) {},
  _exposeMetadatas: Map(0) {},
  _excludeMetadatas: Map(0) {},
  _ancestorsMap: Map(0) {}
}

Maybe there has been some breaking changes to class-transformer? Am I missing something?
@epiphone any ideas?

@epiphone
Copy link
Owner

Couple of things:

  1. Child class must have at least one class-validator decorator for it to get registered in class-transformer's defaultMetadataStorage
  2. refPointerPrefix property must be defined if you want to use reference paths of #/components/schemas instead of the default #/definitions https://github.com/epiphone/routing-controllers-openapi#validation-classes

I've updated the sample project to demonstrate nested validation, please check out epiphone/routing-controllers-openapi@bea6f3d.

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

3 participants