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

Generate schemas in yml format? #384

Closed
takanuva15 opened this issue Oct 23, 2023 · 5 comments · Fixed by #389
Closed

Generate schemas in yml format? #384

takanuva15 opened this issue Oct 23, 2023 · 5 comments · Fixed by #389

Comments

@takanuva15
Copy link
Contributor

takanuva15 commented Oct 23, 2023

Hi, thanks for this great library! I am using the schema autogeneration example here which is perfect for the json validation I am doing in my integration tests. I noticed that all the schemas are generated in json format, which is clunkier than the yml schemas that I'm used to. Is it possible to configure the plugin to generate the json-schema files in yaml format?

For reference, the jackson ObjectMapper I'm using in my code can read yml schema files easily with this configuration:

var objectMapper = new ObjectMapper(new YAMLFactory());

EDIT: I did some more research and I found that the maven plugin writes the jsonSchema to file in this method within SchemaGeneratorMojo.java:

    private void writeToFile(JsonNode jsonSchema, File file) throws MojoExecutionException {
        try (FileOutputStream outputStream = new FileOutputStream(file);
                PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
            writer.print(jsonSchema.toPrettyString());
        } catch (IOException e) {
            throw new MojoExecutionException("Error: Can not write to file " + file, e);
        }
    }

Can we configure this line writer.print(jsonSchema.toPrettyString()); to instead accept any custom ObjectMapper specified by the user to do the writing? I see that the SchemaGeneratorConfigBuilder has an objectmapper associated with it - could we customize that and then use that for the writing so that I can adjust the objectmapper to print out yaml via objectMapper.writeValueAsString(...) ?

@CarstenWickner
Copy link
Member

Hi @takanuva15,

I'm glad the library is of use to you.
I see no reason to prevent this from being generated as YAML files.
Allowing for a custom ObjectMapper sounds good. That merely begs the question of how to declare that.
I suppose the YAML support could be a hard-coded option of the plugin.

Either way, I'm open for a Pull Request. 😃
I don't know when I would get to do something about this myself.

@takanuva15
Copy link
Contributor Author

Thx for the quick reply. I'll have to see when I can find some time to look at the library internals for implementation lol.

In terms of "how to declare it", I was thinking that the SchemaConfigBuilder can define a method .withObjectMapper(...) that would allow the user to pass in their own objectmapper while configuring the builder. Then the writeToFile within the plugin xml can simply call the objectmapper stored in the configbuilder and use that to write to file via objectMapper.writeValueAsString. Does that sound reasonable?

@CarstenWickner
Copy link
Member

The ObjectMapper is already a constructor parameter on the SchemaGeneratorConfigBuilder class.
You propose to make it another dynamic configuration then, in order to allow it to be overridden within the custom Module used by the Maven plugin. 🤔

That'd work, yes. It's somewhat unfortunate to have two ways of configuring it then, but that can't be helped without breaking backward compatibility I suppose.
Switching to the objectMapper.writeValueAsString() also sounds straightforward, yes.

@CarstenWickner
Copy link
Member

After #388 has been merged, this is now possible.
In #389 the documentation is being extended to mention the new capabilities.

@CarstenWickner
Copy link
Member

CarstenWickner commented Oct 27, 2023

Release v4.32.0 has been published, including this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants