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

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

Open
judos opened this issue Jan 3, 2022 · 2 comments

Comments

@judos
Copy link

judos commented Jan 3, 2022

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: #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
Copy link
Author

judos commented Jan 3, 2022

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
Copy link
Author

judos commented Jan 3, 2022

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.

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