[BUG] $ref to common parameter not being resolved
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.
👍 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.
I can't see how to add a label to this issue, but I think it should also have Issue: Regression
@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?
It works fine with 3.3.4.
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
@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.
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)
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.
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.
@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
I assume this happens in all client generators - maybe we can remove the client:HTML tag?
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.
@GeneticGenesis done
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
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.
This seems to fail when in: path but in: query params will work.
The workaround for gradle works: openApiGenerate { .... validateSpec = false }
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
https://github.com/swagger-api/swagger-parser/tree/v2.0.9 is released & should be incorporated.
Please try the latest master with the upgrade parser to see if it's still an issue.
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