RAML 1.0 representation of Java Map
Asked Answered
M

1

68

I have RAML schema which contains "javaType": "java.util.Map<String, java.util.List<Employee>>"

I have separate schema (employee.schema) available which represent class Employee.

But as I have not used employee.schema in RAML so it is not generating class Employee and throws an Error while converting RAML to Java.

{
  "$schema": "http://json-schema.org/schema#",
  "type": "object",
  "description": "Desc",
  "properties": {
    "employeeGroups": {
      "type": "object",
      "javaType": "java.util.Map<String, java.util.List<Employee>>"
    }
  },
  "additionalProperties": false
}

Can anybody share comments how to represent "javaType": "java.util.Map<String, java.util.List<Employee>>" in RAML ?

RAML Version: 1.0
Moira answered 17/5, 2018 at 6:42 Comment(3)
you could use a jsonschema definition of a map, but no sure raml-java-parser will handle it, maybe try first with a simpler map to see if it works, like: "mapInt": { "type": "object", "additionalProperties": {"type": "integer"} }Enrol
You said: "But as I have not used employee.schema in RAML so it is not generating class Employee...". Why would you expect it to work when you are not including one of the dependencies in your RAML? Have you tried adding an include statement to the RAML before generating the Java?Tarp
Have you tried creating a Wrapper class and reference it in the RAML instead the Map one?Unbroken
S
1

You have 2 options here.

The first one is to just include the external file with the schema in the types definition of your raml file.

For instance, assuming that:

  • The object represented in your example is called EmployeeGroupsContainer,
  • And the file with the Employee schema is called employee.schema and is in the same directory as the ramlfile.

The types section would look like this:

types:
    EmployeeGroupsContainer:
        schema: |
                  {
                    "$schema": "http://json-schema.org/schema#",
                    "type": "object",
                    "description": "Desc",
                    "properties": {
                      "employeeGroups": {
                        "type": "object",
                        "javaType": "java.util.Map<String, java.util.List<Employee>>"
                      }
                    },
                    "additionalProperties": false
                  }
    Employee:
        schema:
            !include employee.schema

This would be the recomended approach and the one I would use.

A second option would be to previously generate the Employee object, and once you have it you can generate the rest because the Employee class would now be in your classpath. The best way to do this is by two separate executions of the tool you use to generate the code (the first one with employee.schema to generate the Employee class, and the second one with the rest).

May be you are tempted to generate Employee once and move it to src/main/java, but I would recommend against this, as keeping generated code versioned (in git or any other VCS) is always a bad practice. Code generation should always be a part of the overall build process (tipically with a maven plugin, if you use maven).

The only scenario I can think of to choose the second approach instead of the first one is that you don't have access to the main raml file. But if you do have it, I would definitely go for the first approach.

Sportsman answered 23/12, 2021 at 1:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.