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

How to avoid generating methods for non-getters with the maven plugin? #397

Closed
takanuva15 opened this issue Nov 6, 2023 · 1 comment
Closed

Comments

@takanuva15
Copy link
Contributor

takanuva15 commented Nov 6, 2023

Hi, I am autogenerating a bunch of schemas for my model classes using the maven plugin. I am using lombok to add hashCode and equals methods to my model classes.

I have a class like this:

import lombok.Data;

@Data
public class Dog {
	private int barkVolume;

	public String getBreed() {
		return <calculated-stuff>;
	}
}

When I run the maven plugin with these options:

<options>
  <enabled>
    <option>NONSTATIC_NONVOID_NONGETTER_METHODS</option>
    <option>FIELDS_DERIVED_FROM_ARGUMENTFREE_METHODS</option>
  </enabled>
</options>

...this is the schema that's generated:

properties:
  barkVolume:
    type: integer
  breed:
    type: string
  canEqual(Object):
    type: boolean
  equals(Object):
    type: boolean
  hashCode():
    type: integer
  toString():
    type: string

I'm only interested in generating properties for methods that begin with get...() that take no arguments. How do I get rid of the other stuff that the schema generator is adding in there beyond breed?

(I tested this out on the same MVE repo that I mentioned in my other issue #393.

@CarstenWickner
Copy link
Member

CarstenWickner commented Nov 6, 2023

Hi @takanuva15,

The easiest way would be adding this to your GeneralConfigModule class:

builder.forMethods()
        .withIgnoreCheck(method -> method.getSchemaPropertyName().endsWith(")"));

The Option.FIELDS_DERIVED_FROM_ARGUMENTFREE_METHODS will discard the appended parentheses and "get"/"is" prefix if the method is deemed eligible (i.e., matches get...() or is...() or was explicitly set to another name). If the method's designated name in the schema would still have those parentheses, you can ignore that method again this way.
No need to duplicate the checks being performed in the MethodScope class.

The generated schema looks like this then:

---
$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
  barkVolume:
    type: integer
  breed:
    type: string

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

No branches or pull requests

2 participants