Skip to content

Conversation

yungshinlintw
Copy link
Member

Fix: Handle $ref as JSON object in OpenAPI specifications

Problem

The ObjectPath.FromObject() method assumes that $ref fields in OpenAPI specifications are always strings (references like "#/definitions/FieldType"). However, in some OpenAPI specs, $ref can be a JSON object that defines a schema, causing an InvalidCastException when the code attempts to cast a JObject to a string.

This showed up in the CI run for the following azure-rest-api-specs error below:
https://github.com/Azure/azure-rest-api-specs/actions/runs/18208997652

Reproduction Case

When processing OpenAPI specifications containing schema definitions like:

"FieldDefinition": {
  "type": "object",
  "properties": {
    "$ref": {
      "type": "string",
      "description": "Reference to another field definition."
    }
  }
}

The following error occurs:

System.InvalidCastException: Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken.
at Newtonsoft.Json.Linq.Extensions.ConvertT, U
at Newtonsoft.Json.Linq.Extensions.ValueT, U
at OpenApiDiff.Core.Logging.ObjectPath.FromObject(JObject o, String name)

Root Cause

The original code in FromObject() method attempted to call @ref.Value<string>() without checking if @ref is actually a string. When $ref is a JSON object (schema definition), this causes a cast exception.

Solution

  • Added type checking to verify @ref.Type == JTokenType.String before attempting string conversion

Testing

  • ✅ Run npm run dn.test and verified its pass
  • ✅ Confirmed no regression in existing functionality for string-based $ref values
  • ✅ Tested with the original repro case OpenAPI specifications containing schema definitions in $ref fields

@mikeharder
Copy link
Member

Related: Azure/autorest#5107

@mikeharder mikeharder changed the title Fix: Handle $ref as JSON object in OpenAPI specifications Only treat "$ref" as reference if value is string Oct 13, 2025
@mikeharder mikeharder changed the title Only treat "$ref" as reference if value is string [0.12.3] Only treat "$ref" as reference if value is string Oct 13, 2025
@mikeharder mikeharder changed the title [0.12.3] Only treat "$ref" as reference if value is string [0.12.3] Fix crash in OpenApiDiff.Core.Logging when property named "$ref" Oct 13, 2025
@mikeharder mikeharder changed the title [0.12.3] Fix crash in OpenApiDiff.Core.Logging when property named "$ref" [0.12.3] Fix crash in logging when property named "$ref" Oct 13, 2025
@mikeharder mikeharder moved this from 🤔 Triage to 🔬 Dev in PR in Azure SDK EngSys 🍔🌭 Oct 13, 2025
@mikeharder mikeharder enabled auto-merge (squash) October 13, 2025 18:42
@mikeharder mikeharder disabled auto-merge October 13, 2025 18:42
@mikeharder mikeharder merged commit b5de238 into main Oct 13, 2025
7 checks passed
@mikeharder mikeharder deleted the yungshinlintw/ref_fix branch October 13, 2025 19:03
@kurtzeborn kurtzeborn moved this from 🔬 Dev in PR to 🎊 Closed in Azure SDK EngSys 🍔🌭 Oct 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🎊 Closed

Development

Successfully merging this pull request may close these issues.

3 participants