Skip to content

Improving annotation generation#6

Merged
snake14 merged 19 commits into
5.x-devfrom
PG-4396-generate-oa-annotations
Sep 2, 2025
Merged

Improving annotation generation#6
snake14 merged 19 commits into
5.x-devfrom
PG-4396-generate-oa-annotations

Conversation

@snake14
Copy link
Copy Markdown
Contributor

@snake14 snake14 commented Aug 29, 2025

Description

Making improvements to the annotation generation command. I realised that I should probably get my changes so far reviewed, so I'm creating this PR and branching from it for additional changes.

Issue No

PG-4396

Steps to Replicate the Issue

  1. Run the command like ddev matomo:console openapidocs:generate-annotations --plugin=CustomAlerts to see the annotations for all of the API methods of that plugin generated.
  2. It may take longer since it's making curl calls to demo.matomo.cloud to try and include the examples.
  3. Since CustomAlerts endpoints require the user to be logged in, the lookups will fail and the URL will be used as the example. The field containing the URL is supported by the OpenAPI specs, but Swagger UI doesn't support it yet.

Checklist

  • [✔] Tested locally or on demo2/demo3?
  • [✖] New test case added/updated?
  • [NA] Are all newly added texts included via translation?
  • [NA] Are text sanitized properly? (Eg use of v-text v/s v-html for vue)
  • [✖] Version bumped?

@snake14 snake14 added the Needs Review For pull requests that need a code review. label Aug 29, 2025
@snake14 snake14 requested a review from a team August 29, 2025 07:40
@james-hill-matomo
Copy link
Copy Markdown

@snake14 When testing CustomReports, I get an error:

ddev matomo:console openapidocs:generate-annotations --plugin=CustomReports

            mediaType="application/vnd.ms-excel",
            @OA\Examples(
                example="tsvDemoLink",
                summary="Example tsv",
                value="Error: Data structure returned is not convertible in the requested format: Multidimensional column values not supported. Found unexpected array value for column 'subcategories' in row '0': 'array (
  0 => 
  array (
    'uniqueId' => 'customdimension2',
    'name' => 'Page Author',
  ),
  1 => 
  array (
    'uniqueId' => 'customdimension4',
    'name' => 'Page Location',
  ),
  2 => 
  array (
    'uniqueId' => 'customdimension5',
    'name' => 'Page Type',
  ),
  3 => 
  array (
    'uniqueId' => 'VisitorInterest_Engagement',
    'name' => 'Engagement',
  ),
  4 => 
  array (
    'uniqueId' => 'Transitions_Transitions',
    'name' => 'Transitions',
  ),
  5 => 
  array (
    'uni'. Try to call this method with the parameters '&format=original&serialize=1'; you will get the original php data structure serialized."
            )
        )
    ),
    @OA\Response(response=400, ref="#/components/responses/BadRequest"),
    @OA\Response(response=401, ref="#/components/responses/Unauthorized"),
    @OA\Response(response=403, ref="#/components/responses/Forbidden"),
    @OA\Response(response=404, ref="#/components/responses/NotFound"),
    @OA\Response(response=500, ref="#/components/responses/ServerError"),
    @OA\Response(response="default", ref="#/components/responses/DefaultError"),
    x={"runtime"={"entry":"index.php","query":{"module":"API","method":"CustomReports.getAvailableCategories"}}}
)

@james-hill-matomo
Copy link
Copy Markdown

@snake14

image

$categoryId is (incorrectly) typed as string in the docblock, and in the function signature it defaults to false. Not sure the best way of solving this, or if we even should in the automated generation.

@james-hill-matomo
Copy link
Copy Markdown

@snake14 Default query param of '' is encoded as """"

image image

@snake14 apologies - this CR isn't scoped to your changes well.

Comment thread Annotations/AnnotationGenerator.php Outdated
Comment on lines +53 to +56
$pluginConfigPath = $pluginDir . '/OpenApi/Annotations/config.php';
if (is_file($pluginConfigPath)) {
$pluginRules = require $pluginDir . '/OpenApi/Annotations/config.php';
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe nitpick (does the security scanner pick up on this?) but if this is always going to be arrays, it'd be much more secure to have this as json or yaml or something rather than requiring a php.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Are you planning for us to customise each plugin in pluginDir/OpenApi/Annotations/config.php instead of directly editing the file? Or is this for something else?

Copy link
Copy Markdown
Contributor Author

@snake14 snake14 Sep 1, 2025

Choose a reason for hiding this comment

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

@james-hill-matomo As mentioned in Slack, I'm trying to remove the use of a config file. The main thing that I haven't determined how to do without a config is whether to use GET or POST for each API method. I guess I can just have everything show as GET for now.

Comment thread Annotations/AnnotationGenerator.php Outdated

$mediaTypes = [];
// This simply reuses the example URLs used by the current documentation, but some endpoints don't work because authentication is required
// TODO - Come up with a way to demo examples for endpoints which require authentication. E.g. hit a live endpoint server-side and replace any potentially sensitive data...
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That can stay out of scope for now. I don't think the demos will be so important if we can provide a live swagger that users can query their own data with easily.

Comment on lines +389 to +392
if ($default !== '') {
// TODO - Add some logic to only add default if it matches the type. E.g. false isn't a good default for string
$schemaMap[] = 'default="' . $default . '"';
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hah - Shame I didn't read this before my other comments about the defaults.

@snake14
Copy link
Copy Markdown
Contributor Author

snake14 commented Sep 1, 2025

@snake14 When testing CustomReports, I get an error:

ddev matomo:console openapidocs:generate-annotations --plugin=CustomReports

Ah. Yeah. Probably related to Matomo APIs returning 200 type responses when they should be 4xx or 5xx. I can add an extra check.

Edit: With the latest commit, annotations are built successfully for the CustomReports plugin.

Comment thread Annotations/AnnotationGenerator.php

protected function getExampleIfAvailable(string $url): array
{
$ch = curl_init($url);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can't we use Http::sendHttpRequestBy instead of using curl directly ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@AltamashShaikh Possibly. I forgot that was an option and didn't see it when I was looking for examples in the codebase.

Copy link
Copy Markdown
Contributor

@AltamashShaikh AltamashShaikh left a comment

Choose a reason for hiding this comment

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

@snake14 Left few comments overall looks good to me.

@snake14
Copy link
Copy Markdown
Contributor Author

snake14 commented Sep 1, 2025

@snake14 Default query param of '' is encoded as """"

I just tested Swagger UI and it shows Default: '' instead of Default: , which seems preferable to me.

@snake14
Copy link
Copy Markdown
Contributor Author

snake14 commented Sep 1, 2025

@james-hill-matomo @AltamashShaikh Any concerns with the last 2-3 commits or am I good to merge once #7 is merged in?

Copy link
Copy Markdown
Contributor

@AltamashShaikh AltamashShaikh left a comment

Choose a reason for hiding this comment

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

@snake14 I can see the file being created with not-dry-run option

@snake14 snake14 merged commit 2bfe31a into 5.x-dev Sep 2, 2025
7 checks passed
@snake14 snake14 deleted the PG-4396-generate-oa-annotations branch September 2, 2025 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Review For pull requests that need a code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants