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

Parsing Javadoc comments to generate descriptions #485

Open
twobiers opened this issue Jun 12, 2021 · 6 comments
Open

Parsing Javadoc comments to generate descriptions #485

twobiers opened this issue Jun 12, 2021 · 6 comments

Comments

@twobiers
Copy link

Hi,

I wonder if it makes sense to include the parsing of javadoc comments to create descriptions for schemas, paths, parameters, etc. in the standard.

The non-standard implementation micronaut-openapi does exactly that.

As a user, this would be a totally useful feature from my point of view.

@Azquelt
Copy link
Member

Azquelt commented Feb 14, 2022

This isn't something can be done by an implementation that scans classes at runtime (as OpenLiberty does) as the javadoc comments aren't available at runtime.

It also wouldn't work if you were using a third party jar unless it also includes the source code.

There's no reason that this functionality couldn't be provided by any implementation that operates at build time and has the source files available, but I wouldn't want it to be required by the spec.

@liefke
Copy link

liefke commented Mar 15, 2022

I asked the same question at Stackoverflow: Generate OpenAPI descriptions from JavaDoc

I'm already serializing the JavaDoc during buildtime, so I guess I would need to implement my own OASFactoryResolver to offer the JavaDoc at runtime to the OpenAPI?

@MikeEdgar
Copy link
Member

MikeEdgar commented Mar 15, 2022

@liefke if you have made the comments available to your application at runtime in some format, you would probably want to use the OASModelReader to generate the initial OpenAPI model based on that data. In that case you may want to turn off annotation scanning with mp.openapi.scan.disable=true.

@liefke
Copy link

liefke commented Mar 15, 2022

@MikeEdgar thanks for the clarification.

As you also are part of the SmallRye-Team, I hope that you can even answer my next answer, which is about integration into WildFly:
As far as I understand the JavaDoc comment of OASModelReader (which needs to be visible to the application's classloader), I need to put my OASModelReader implementation into a WildFly-module? Or are the deployments scanned for such an implementation, like annotation scanning does, too?

@MikeEdgar
Copy link
Member

@liefke you can place the OASModelReader implementation in your deployment/application (or a jar in the app's class path) and you'll need to include the mp.openapi.model.reader property with the fully-qualified class name of the reader.

Azquelt pushed a commit to Azquelt/microprofile-open-api that referenced this issue Mar 17, 2022
….org.jboss.resteasy-4.5.8.Final

Bump version.org.jboss.resteasy from 4.5.7.Final to 4.5.8.Final
@liefke
Copy link

liefke commented Mar 23, 2022

@MikeEdgar Thank you, I got it running.

But if you read the original question of @tobi6112 you will notice, that we don't want to replace the OpenApi annotation parsing with our own implementation, but we only want to add the JavaDoc as description. So the OASFilter (property mp.openapi.filter in the file META-INF/microprofile-config.properties) is the much better solution.

Example:

public class JavadocOASDescriptionFilter implements OASFilter {
	@Override
	public void filterOpenAPI(final OpenAPI openAPI) {
		openAPI.getComponents().getSchemas().forEach(this::initializeSchema);
		openAPI.getPaths().forEach(this::initializePathItem);
	}

	private void initializeSchema(final String name, final Schema schema) {
		final SerializedJavadoc javadoc = findJavadocForSchema(name);
		if (StringUtils.isEmpty(schema.getDescription())) {
			schema.setDescription(javadoc.getTypeComment());
		}
		if (schema.getProperties() != null) {
			schema.getProperties().forEach((property, propertySchema) -> {
				if (StringUtils.isEmpty(propertySchema.getDescription())) {
					propertySchema.setDescription(javadoc.getAttributeComments().get(property));
				}
			});
		}
	}
	...
}

Side note: I could only override filterOpenAPI, because all other convenient filter methods do not provide the key of the corresponding map, e.g. filterSchema(Schema) doesn't include the name of the schema.

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

4 participants