OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.24k stars 6.43k forks source link

[BUG] `$ref` does not work under `paths/<uri>/<method>/parameters/<param>/schema` #7897

Open BenceSzalai opened 3 years ago

BenceSzalai commented 3 years ago

Bug Report Checklist

Description

According to OpenAPI Specification v3.0.3 $ref is allowed to define the schema of a Parameter object, however when doing so code generation generates warnings such as WARN o.o.codegen.utils.ModelUtils - #/components/schemas/... is not defined, while the $ref pointer is definitely correct, for example it can be resolved fine by a variety of other tools.

openapi-generator version

This behaviour is the same for both v5.0.0-beta2 and v4.3.1. I've also checked against the latest snapshot.

OpenAPI declaration file content or url

Full example: https://gist.github.com/BenceSzalai/908243fbe9a3107a11bf9093c77e31bc

Relevant excerpt:

paths:
  /offer/:
    get:
      parameters:
        - name: country
          schema:
            $ref: '#/components/schemas/Offer/properties/country'
components:
  schemas:
    Offer:
      type: object
      properties:
        country:
          type: string
Steps to reproduce
  1. Use the sample yaml provided above as the OpenAPI specification file
  2. run the local equivalent of /bin/java -jar openapi-generator.jar generate -g php -i "sample.yaml" -o "generated" Actually the chosen generator doesn't matter, -g php is only an example.*
  3. Observe [main] WARN o.o.codegen.utils.ModelUtils - #/components/schemas/Offer/properties/country is not defined emitted during generation.

*: In fact it exposes the issue more, since the $country parameter on DefaultApi::getOffer() in generated/lib/Api/DefaultApi.php gets type hinted with a non existent class: \OpenAPI\Client\Model\Country instead of string which should have been resolved from the $ref. But probably it has further consequences in other generators as well.

BenceSzalai commented 3 years ago

Meanwhile I've figured out that everything works fine, if the $ref points to a schema object, not a property of it. So apparently the resolution works at least in part, because this is fine:

paths:
  /offer/:
    get:
      parameters:
        - name: country
          schema:
            $ref: '#/components/schemas/Country'
components:
  schemas:
    Offer:
      type: object
      properties:
        country:
          $ref: '#/components/schemas/Country'
    Country:
      type: string

According to the OpenAPI specs the references should work as defined by JSON Reference, so generally there's no restriction for parameter schema references to only allow pointing to direct children of #/components/schemas, but not to the descendant of it.

tigerinus commented 3 years ago

Hit the same issue. Would be nice to have this solved.