kin-openapi icon indicating copy to clipboard operation
kin-openapi copied to clipboard

Roundtripping T via JSON leaves requestBody properties without populated Value

Open andig opened this issue 5 months ago • 1 comments

What am I doing: parse a spec containing requestBody into an openapi3.T:

    "/session/{id}": {
      "put": {
        "description": "Update vehicle of charging session. [Read more.](https://docs.evcc.io/en/docs/features/sessions)\n",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "properties": {
                  "loadpoint": {
                    "$ref": "#/components/schemas/LoadpointName"
                  },
                  "vehicle": {
                    "$ref": "#/components/schemas/VehicleName"
                  }
                },
                "type": "object"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Success"
          }
        },
        "summary": "Update vehicle of charging session",
        "tags": [
          "Sessions"
        ]
      }
    },

Everything is fine working with this T, specifically the requestBodys schema parameters are fully populated, i.e. their Value is non-nil.

After roundtripping this openapi.T via JSON, the requestBodys schema parameter's Values are nil. It seems as if something is missing in the JSON unmarshaling regarding fully resolving the schema references.

This sounds a lot like both https://github.com/getkin/kin-openapi/issues/1078 and also https://github.com/getkin/kin-openapi/issues/1074.

Since this issue is blocking us from caching a parsed API spec instead of parsing it every time I'd be happy to sponsor getting this fixed.

andig avatar Jun 30 '25 20:06 andig

Seems this can be resolved using a Loader:

	var doc *openapi3.T
	if err := json.Unmarshal(spec, &doc); err != nil {
		return nil, fmt.Errorf("failed to load OpenAPI spec: %v", err)
	}

	if err := openapi3.NewLoader().ResolveRefsIn(doc, nil); err != nil {
		return nil, fmt.Errorf("failed resolving spec references: %v", err)
	}

Leaving open for discoverability/ comments.

andig avatar Jun 30 '25 21:06 andig