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

[BUG] $ref to common parameter not being resolved

Open john3300 opened this issue 7 years ago • 34 comments

Description

Validation is failing to resolve a $ref in the parameter list to a common parameter defined outside the operation. Neither refs local to the same file nor refs to external files work. Both should. I've tried it with both Swagger 2.0 and OAS 3.0 specs, both fail the same way.

Exception in thread "main" org.openapitools.codegen.SpecValidationException: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI).
 | Error count: 1, Warning count: 0
Errors: 
	-attribute paths.'/employees/{id}'. Declared path parameter id needs to be defined as a path parameter in path or operation level

	at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:569)
	at org.openapitools.codegen.cmd.Generate.run(Generate.java:346)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:62)
openapi-generator version

4.0.0-SNAPSHOT

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Test
  description: Test YAML
  version: '1.0'
servers:
  - url: http://localhost
paths:
  /employees/{id}:
    get:
      operationId: getEmployeeById
      parameters:
        - $ref: '#/components/parameters/id'
      responses:
        200:
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/employee'
components:
  schemas:
    employee:
      type: object
      properties:
        name:
          type: string
      required:
        - name
  parameters:
    id:
      name: id
      in: path
      required: true
      schema:
        type: string
Command line used for generation

java -jar openapi-generator-cli-4.0.0-20181217.102953-101.jar generate -g html -i test.yaml

Related issues/PRs

#455 seems to be related. But that one was resolved. So that makes this a regression.

Suggest a fix

This could be another issue with the swagger-parser like in #455.

john3300 avatar Dec 19 '18 16:12 john3300

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

Please review the labels and make any necessary changes.

auto-labeler avatar Dec 19 '18 16:12 auto-labeler

I can't see how to add a label to this issue, but I think it should also have Issue: Regression

john3300 avatar Dec 19 '18 16:12 john3300

@john3300 Did this work with another tool or a previous version of the generator? It seems to me this is an issue with swagger parser.

If it previously worked, can you provide the previous version of openapi-generator so we can look at the regression?

jimschubert avatar Dec 19 '18 20:12 jimschubert

It works fine with 3.3.4.

john3300 avatar Dec 20 '18 16:12 john3300

FYI if you expand the ref the issue goes away (not that it helps much):

openapi: 3.0.0
info:
  version: 1.0.0
  title: |
    This fails to generate code when the path paramter is defined via schema $ref
     - attribute paths.'/bar/{id}'. Declared path parameter id needs to be defined as a path parameter in path or operation level

paths:
  '/foo/{id}':
    get:
      summary: Returns the foo of a prior request
      operationId: fooGet
      parameters:
        -
          name: id
          in: path
          required: true
          description: ID returned from a prior POST operation of the same name
          schema:
            type: string
      responses:
        '200':
          description: A list of predicted text boxes
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/FooBarResponse'
  '/bar/{id}':
    get:
      summary: Returns the bar of a prior request
      operationId: barGet
      parameters:
        - $ref: '#/components/parameters/ID'
      responses:
        '200':
          description: A list of predicted text boxes
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/FooBarResponse'

components:
  parameters:
    ID:
      name: id
      in: path
      required: true
      description: ID returned from a prior POST operation of the same name
      schema:
        type: string
  schemas:
    FooBarResponse:
      type: object
      properties:
        foo:
          type: string
        bar:
          type: string

ericraymond avatar Dec 26 '18 23:12 ericraymond

@john3300 can you try with --skip-validate-spec? I tried to repeat the issue with the spec you provided but couldn't after using the --skip-validate-spec option.

wing328 avatar Dec 30 '18 10:12 wing328

Above test file works via the gradle plugin with validateSpec = false. It complains about the error, but seems to continue.

Larger test case is failing on another issue. This test case would further test the generated code. Will investigate and comment/log bugs as necessary. (Bug might be same as #1657)

ericraymond avatar Dec 31 '18 20:12 ericraymond

As requested by @wing328 I tested with --skip-validate-spec and got the same results as @ericraymond. It lists the error but completes the work.

Obviously that's not a valid long term solution for something that worked in the previous release.

john3300 avatar Jan 07 '19 16:01 john3300

I can confirm this happens with 4.0.0 nightly and JavaScript generator.

I like the validation, so we'll wait until it is working before migrating to 4.0.0.

advance512 avatar Jan 07 '19 19:01 advance512

@john3300 Did this work with another tool or a previous version of the generator? It seems to me this is an issue with swagger parser.

If it previously worked, can you provide the previous version of openapi-generator so we can look at the regression?

This issue was carried over from https://github.com/swagger-api/swagger-parser/issues/911.

Upstream has an open PR: https://github.com/swagger-api/swagger-parser/pull/977

jjlin avatar Jan 10 '19 01:01 jjlin

I assume this happens in all client generators - maybe we can remove the client:HTML tag?

philcluff avatar Jan 16 '19 13:01 philcluff

Obviously that's not a valid long term solution for something that worked in the previous release.

@john3300 please use --skip-validate-spec for the time being until the issue (false alarm) is addressed in the swagger parser.

wing328 avatar Jan 16 '19 14:01 wing328

@GeneticGenesis done

wing328 avatar Jan 16 '19 14:01 wing328

I tried this with the new 4.0.0 beta2 version and I could still reproduce this problem. I get error messages like: -attribute paths.'/accounts/{account-id}/transactions'. Declared path parameter account-id needs to be defined as a path parameter in path or operation level

zslajosfalvi avatar Feb 01 '19 07:02 zslajosfalvi

I believe this could be the fix? Hoping it gets merged soon. https://github.com/swagger-api/swagger-parser/pull/977

My team is in the same boat as @advance512, we are blocked from upgrading to 4.0.0 beta2 until this is addressed.

fantavlik avatar Feb 01 '19 17:02 fantavlik

This seems to fail when in: path but in: query params will work.

The workaround for gradle works: openApiGenerate { .... validateSpec = false }

tcjcodes avatar Feb 12 '19 01:02 tcjcodes

https://github.com/swagger-api/swagger-parser/pull/977 has now been merged into master of swagger-parser.

Can we pull in the latest swagger-parser and re-test?

Thanks

philcluff avatar Feb 20 '19 11:02 philcluff

https://github.com/swagger-api/swagger-parser/tree/v2.0.9 is released & should be incorporated.

ntfrnzn avatar Feb 26 '19 20:02 ntfrnzn

Please try the latest master with the upgrade parser to see if it's still an issue.

wing328 avatar Mar 29 '19 16:03 wing328

Thanks @wing328

This seems to have fixed this specific issue, but sadly ref'd parameters have now been broken by https://github.com/swagger-api/swagger-parser/issues/1063

philcluff avatar Apr 03 '19 14:04 philcluff