routing-controllers-openapi icon indicating copy to clipboard operation
routing-controllers-openapi copied to clipboard

nested maps, multi-dimensional arrays not able to annotate with type information

Open judos opened this issue 4 years ago • 2 comments

It seems impossible to annotate nested maps with type information. Consider this property:

data?: Map<string, Map<string, MyDto>>;

Now if it were only a single map I can correctly tell openapi it should validate the nested objects, and what type they are:

@ValidateNested({ each: true })
@Type(() => MyDto)
data?: Map<string, MyDto>;

But what I have is two level of maps inside each other.

1.) I considered creating a separate object for one map with type, but this also doesn't help, because I can't add type information on a class. e.g.:

@ValidateNested({ each: true })
@Type(() => MyObject)
data?: Map<string, MyObject>;

// Some other file:
// FIXME: Annotations on class level don't work, so no possibility to add inner type information.
export class MyObject extends Map<string, MyDto> {
}

Does anyone know how to do this?

2.) Second workaround would be to manually define the JSONSchema with annotation:

@ValidateNested({ each: true })
// FIXME: Help, how can I correctly add type information for this field?
// @JSONSchema({
//   format: 'custom-test',
//   type: 'object'
// })
data?: Map<string, Map<string, MyDto>>;

The same issue also occurs with other nested structures, such as arrays inside a map. See also: https://github.com/epiphone/routing-controllers-openapi/issues/74

The documentation didn't mention such specific cases: https://github.com/typestack/class-transformer#working-with-nested-objects https://github.com/epiphone/class-validator-jsonschema#validatenested-and-arrays

Thanks for your help!

judos avatar Jan 03 '22 15:01 judos

Workaround found is to define JSONSchema manually. I did as follows:

@JSONSchema({
  description: '...',
  properties: {
    '{itemId}': {
      type: 'object',
      description: '',
       properties: {
        '{attributeName}': {
          $ref: '#/components/schemas/MyDto'
        }
      }
    }
  },
  type: 'object'
})
@Type(() => MyDto) // to make sure schema is listed, otherwise reference doesn't work
data: Map<string, Map<string, MyDto>>;

judos avatar Jan 03 '22 16:01 judos

Is there a simpler way to annotate such complex nested types with routing-controllers-openapi? I have the feeling an annotation could solve this a lot easier for everyone.

judos avatar Jan 03 '22 16:01 judos