jsonschema-generator icon indicating copy to clipboard operation
jsonschema-generator copied to clipboard

Maven plugin - Generating schemas for multiple classes

Open JanLabrie opened this issue 6 years ago • 8 comments

The current Maven plugin only allows to generate the schema for one class. This should be extended to specify multiple classes. Either by mentioning them or by mentioning a package.

JanLabrie avatar Apr 22 '20 11:04 JanLabrie

As per comment on #78, I'd propose a structure like this:

<configuration>
    <targetTypes>
        <targetType>
            <className>a.b.c.YourClass</className>
        </targetType>
        <targetType>
            <className>a.b.c.MyClass</className>
            <schemaFileName>MyClass_schema.json</schemaFileName>
        </targetType>
    </targetTypes>
    <targetPackages>
        <targetPackage>
            <packageName>x.y.z</packageName>
        </targetPackage>
    </targetPackages>
    <schemaFilePath>src/main/resources/schema</schemaFilePath>
</configuration>

CarstenEnglert avatar Apr 22 '20 12:04 CarstenEnglert

I'll go for a slightly different approach:

<configuration>
    <targetTypes>
            <className>a.b.c.YourClass</className>
            <className>a.b.c.MyClass</className>
    </targetTypes>
    <targetPackages>
            <packageName>x.y.z</packageName>
    </targetPackages>
    <schemaFilePath>src/main/resources/schema</schemaFilePath>
    <schemaFileNamePattern>schema-%s.json</schemaFileNamePattern>
</configuration>

The filename pattern is applied to all generated files, and uses the syntax of String.format, where the class name is the one parameter.

JanLabrie avatar Apr 24 '20 09:04 JanLabrie

Should be good enough for now, but will be harder to extend later on if the request comes for setting individual names and naming patterns per type/package.

CarstenEnglert avatar Apr 24 '20 09:04 CarstenEnglert

Let's build it and gain experience. I am always a fan of defining a pattern and sticking to it.

One other topic on the schema files for packages: shall we generate them in a subfolder that matches the package name? Mainly to prevent name collisions.

JanLabrie avatar Apr 24 '20 10:04 JanLabrie

That subfolder would only be necessary if the same type names are present in two packages. Possible, but probably not very likely that someone would want to generate schemas for both types then.

Maybe we can use MessageFormat.format() instead of String.format() and thereby offer multiple numbered placeholder values.

  • {0} would be the simple class name
  • {1} would be the package name

We would be flexible to add more values later on and the user could simply include the package name in the file name themselves, if there are any conflicts otherwise.

CarstenEnglert avatar Apr 24 '20 10:04 CarstenEnglert

Agreed. The default can then be: {0}-schema.json

JanLabrie avatar Apr 24 '20 10:04 JanLabrie

I already included the discussed naming pattern in #88, but not the ability to configure multiple classes though.

CarstenEnglert avatar Apr 27 '20 04:04 CarstenEnglert

The work is done. PR submitted. The xml turned out with some different names, but the same structure. Have your go at it.

JanLabrie avatar Apr 28 '20 12:04 JanLabrie