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

$ref generates classes in wrong package #1543

Open
LitschiW opened this issue Jul 17, 2023 · 3 comments
Open

$ref generates classes in wrong package #1543

LitschiW opened this issue Jul 17, 2023 · 3 comments

Comments

@LitschiW
Copy link

LitschiW commented Jul 17, 2023

Hi there,

love the project so far, but I just ran into a weird problem.
The issue is rather similar to #410, which was closed some years ago. However, I'm still experiencing a very similar issue using the maven plugin version 1.2.1.

When having 3 sibling directories, of which 2 references the third, running jsonschema2pojo will generate the classes of the third (dependencyless) directory in the package of the first reference.


Example

Input:

  • parent_dir
    • dir1
      • schema1.json
    • dir2
      • schema2.json
    • dir3
      • schema3.json

Schemas 1 and 2 have a $ref to ../dir3/schema3.json#.

Output:

  • parent_dir
    • dir1
      • schema1.java
      • schema3.java
      • ... (all other by-product classes of schema1 and schema3)
    • dir2
      • schema2.java

The Imports are resolved correctly and this compiles fine. However, I would expect schema3 to be generated in a package dir3.
Also note that the existence of dir2 is not relevant to demonstrate the problem, but rather to show that the classes will be generated in the same package as their first reference.


I have the following plugin configuration:

 <plugin>
                <groupId>org.jsonschema2pojo</groupId>
                <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <sourceDirectory>${basedir}/../../schemas</sourceDirectory>
                    <includes>
                        <include>**/*.json</include>
                    </includes>
                    <outputDirectory>${basedir}/src/main/java/</outputDirectory>

                    <targetPackage>dqualizer.dqlang.types</targetPackage>
                    <removeOldOutput>false</removeOldOutput>
                    <useTitleAsClassname>true</useTitleAsClassname>

                    <useOptionalForGetters>true</useOptionalForGetters>
                    <usePrimitives>true</usePrimitives>

                    <includeSetters>false</includeSetters>
                    <includeGetters>false</includeGetters>


                    <includeConstructors>true</includeConstructors>
                    <includeConstructorPropertiesAnnotation>true</includeConstructorPropertiesAnnotation>
                    <includeAllPropertiesConstructor>true</includeAllPropertiesConstructor>
                    <includeCopyConstructor>true</includeCopyConstructor>


                    <includeJsr303Annotations>true</includeJsr303Annotations>
                    <includeJsr305Annotations>true</includeJsr305Annotations>

                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

If you want to see this in practice, you can take a look at our repository. By now we have moved away from jsonschema2pojo but you can still reproduce the bug:

git clone -b Insturmentation_Definition git@github.com:dqualizer/dqlang.git
cd dqlang
git checkout tags/v0.3.1 generators/java && cd generators/java && mvn compile && cd ../..

After that, you can see that there is a schemas/architecture/service.json files which uses $ref to references (transitively) all schemas from schemas/instrumentation. The generation output which is under generators/java/src/main/java/dqualizer/dqlang/types then contains an architecture package that includes ServiceDescription.java (as expected) but also all classes generated from the schemas of schemas/instrumentation which I would expect to be in a package instrumentation.
If you want to play around further with this; add a ref to the instrumentation schema to a schema in the adapter folder and see that the classes are now generated in the adapter package, but will be referenced correctly from the classes that reside in the architecture package.

@unkish
Copy link
Collaborator

unkish commented Jul 21, 2023

Hi

If you want to see this in practice, take a look at https://github.com/dqualizer/dqlang/tree/Insturmentation_Definition/generators/java (be aware that this is not the main branch.)

Not quite sure what to look at as there are a lot of schemas that end up generating 58 files.

@LitschiW
Copy link
Author

Hi there. Sry I forgot to point out these details. I Updated the description accordingly.

@unkish
Copy link
Collaborator

unkish commented Aug 4, 2023

I think that #410 may have been closed as solution was to use javaType extension property to make output predictable/deterministic.
There's a good explanation of how/why it works like that here as well as suggestion how to change the behavior:

For now though, you can see that without javaType you can get some unexpected results

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

2 participants