Fix NullPointerException in MarkdownRender when schema is null

Merged
CO
10mo ago
+43-3
  • Understand the issue: NullPointerException in MarkdownRender when schema is null (similar to issue #865 which was fixed for HtmlRender)
  • Identify the root cause: itemContent method in MarkdownRender.java at line 276-278 passes potentially null schema to schema() method which doesn't handle null
  • Add null check in MarkdownRender.itemContent() method to handle null schema
  • Identify and fix the same issue in AsciidocRender and ConsoleRender
  • Add test cases for null schema scenario in MarkdownRenderTest, AsciidocRenderTest, and ConsoleRenderTest
  • Use issue ID prefix in test method names (e.g., issue870_renderDoesNotFailWhenSchemaIsNullButExampleChanged)
  • Run tests to verify the fixes work correctly
  • Run code review - only nitpick found (StringBuilder vs string concatenation), kept current implementation for consistency with codebase
  • Run security scan - no vulnerabilities found
Original prompt

This section details on the original issue you should resolve

<issue_title>NullPointerException in Markdown Renderer</issue_title>
<issue_description>

Description

The --markdown output option fails with a NullPointerException when generating markdown reports, while the console output and --json output work correctly.

Steps to Reproduce

  1. Run openapi-diff with markdown output:
docker run --rm -v $(pwd):/specs openapitools/openapi-diff:latest \
  /specs/swagger.json /specs/main.json --markdown /specs/report.md
  1. The command fails with the error below, despite the console output showing the comparison completed successfully.

Expected Behavior

The markdown file should be generated successfully, similar to how the console output and JSON output work.

Actual Behavior

The command throws a NullPointerException:

java.lang.NullPointerException: Cannot invoke "org.openapitools.openapidiff.core.model.ChangedSchema.isDiscriminatorPropertyChanged()" because "schema" is null
	at org.openapitools.openapidiff.core.output.MarkdownRender.schema(MarkdownRender.java:289)
	at org.openapitools.openapidiff.core.output.MarkdownRender.itemContent(MarkdownRender.java:248)
	at org.openapitools.openapidiff.core.output.MarkdownRender.lambda$bodyContent$7(MarkdownRender.java:222)

Environment

  • Docker Image: openapitools/openapi-diff:latest
  • OpenAPI Version: 3.0.1
  • Date Tested: November 27, 2025

Console Output (Successful)

When running without --markdown, the tool works correctly:

==========================================================================
==                            API CHANGE LOG                            ==
==========================================================================
                         Targeting.DayPartingApi
--------------------------------------------------------------------------
--                            What's Changed                            --
--------------------------------------------------------------------------
- PUT    /targeting
  Request:
        - Changed application/json
          Schema: Backward compatible
        - Changed text/json
          Schema: Backward compatible
        - Changed application/*+json
          Schema: Backward compatible
--------------------------------------------------------------------------
--                                Result                                --
--------------------------------------------------------------------------
                   API changes are backward compatible
--------------------------------------------------------------------------

Root Cause Analysis

The error occurs in MarkdownRender.java at line 289, where it attempts to call isDiscriminatorPropertyChanged() on a null ChangedSchema object. The markdown renderer appears to be less tolerant of null schemas compared to the console renderer.

The issue specifically arises when:

  • The API has schema changes in request/response bodies
  • The ChangedSchema object is null for certain content types
  • The markdown renderer tries to check all schema properties without null-checking

Workarounds

  1. Use JSON output instead:
docker run --rm -v $(pwd):/specs openapitools/openapi-diff:latest \
  /specs/swagger.json /specs/main.json --json /specs/report.json
  1. Use console output and parse manually:
docker run --rm -v $(pwd):/specs openapitools/openapi-diff:latest \
  /specs/swagger.json /specs/main.json --info > output.txt

Impact

  • Severity: Medium
  • Affected Feature: Markdown output generation (--markdown flag)
  • Impact: Users cannot generate markdown reports but can use JSON or console output as alternatives

Additional Context

This issue was discovered when comparing two OpenAPI 3.0.1 specifications where the request body examples changed but the schemas remained the same. The tool correctly identifies the changes as backward compatible but fails during markdown generation.
</issue_description>

<agent_instructions>Looks like the same problem https://github.com/OpenAPITools/openapi-diff/issues/865</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@DrSatyr Looks similar to https://github.com/OpenAPITools/openapi-diff/issues/865

šŸ’¬ We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.