Skip to content

Sanitize additionalProperties with CliOptions #7746

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Zomzog
Copy link
Contributor

@Zomzog Zomzog commented Oct 18, 2020

fix #6591

It's modifying the cli so I ping the core team :
@wing328
@jimschubert
@cbornet
@ackintosh
@jmini
@etherealjoy
@spacether

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • If contributing template-only or documentation-only changes which will change sample output, build the project beforehand.
  • Run the shell script ./bin/generate-samples.shto update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

@Zomzog Zomzog marked this pull request as ready for review October 18, 2020 22:32
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

/**
* A class which manages the contextual configuration for code generation.
* This includes configuring the generator, templating, and the workflow which orchestrates these.
*
* <p>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed

return input.openAPI((OpenAPI) context.getSpecDocument());
}

private Object sanitizeAdditionalPropertyValue(CodegenConfig config, String opt, Object value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't sanitization, but coercion from string properties to those declared in CLI options.

I love the idea behind this, but I have quite a few reservations.

I think this is potentially going to become very confusing for folks because additionalProperties is intended for users to be able to pass extra properties to templates. We just happen to also use it for generator's options to be passed to templates as well. What happens for a user who wants to pass a custom property like isSupported: "false"? We now have some properties as boolean and some as string.

It's generally up to generators themselves to determine whether or not a boolean value should be type bool in the template or type string, and this change could potentially break templates which expect string true/false in templates rather than bools. There are about 65 places where this might be affected in code, not counting built-in templates. We may also potentially be breaking some languages where booleans are cased differently (like Python), or even support them being worded differently (yes and no in YAML). In those cases, any sort of check like if ("false".equals(property)) would now fail, and we'd have to be certain our testing covers all of those permutations of options.

I'd much rather fix this specific instance of reactive: false at the source of the problem in SpringCodegen with this code:

        if (additionalProperties.containsKey(REACTIVE)) {
            if (!SPRING_BOOT.equals(library)) {
                throw new IllegalArgumentException("Currently, reactive option is only supported with Spring-boot");
            }
            convertPropertyToBooleanAndWriteBack(REACTIVE);
            this.setReactive((boolean)additionalProperties.get(REACTIVE));
        }

and figure out a way separately for generators to opt-in to automatically coercing CliOpts to their defined types in a way which is appropriate for the target languages.

I love the idea you've used, and maybe we could move it to DefaultCodegen with a coerceAdditionalProperties flag which defaults to false, but when set to true invokes this logic? I guess that would be another potential change I'd feel comfortable with if we set the spring generator to true here. But, I think that would also mean cleaning up all the convertPropertyToBooleanAndWriteBack usages in the Spring generator as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think I understand the idea, so I move the check to a method in SpringCodegen and AbstractJavaCodegen. The impact on samples is due to the fact I set default values on additionalProperties so now everything is java8 by default.

The idea is that each generator provides his list of properties to check/convert to boolean so custom properties will stay has to String if needed.

Let me know if it's an acceptable solution for you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jimschubert I've reworked the solution when I understand that default values can be modified. I know there is a test fail on CI with java8 and maybe CliOption must be reworked to use the "defaultValue" method. But I hope you can confirm I'm on the right way before pushing further.

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

Successfully merging this pull request may close these issues.

[BUG] spring generator using maven plugin generates correct code, while cli/gradle generates wrong code
2 participants