[BUG] Using $ref under components/schema results in spec validation failure
A spec like this:
openapi: 3.0.3
info:
title: api spec
version: 1.0.0
servers:
- url: /v3
components:
schemas:
$ref: './definitions/_index.yaml'
...
Results in validation error(s):
xception 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: 2, Warning count: 0
Errors:
-attribute components.schemas.$ref is not of type `object`
-attribute components.schemas.Schema name $ref doesn't adhere to regular expression ^[a-zA-Z0-9\.\-_]+$
at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:544)
at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:571)
at org.openapitools.codegen.cmd.Generate.execute(Generate.java:433)
at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
This is using docker.io/openapitools/openapi-generator-cli:latest (image id: e718a88b4de4). The spec with the schemas changed as below passes the validation:
openapi: 3.0.3
info:
title: api spec
version: 1.0.0
servers:
- url: /v3
components:
schemas:
UnauthorizedError:
$ref: "./definitions/unauthorized_error.yaml"
NotFoundError:
$ref: "./definitions/not_found_error.yaml"
...
According to 3.0 docs a $ref element should be valid immediately under schema.
Have been hit by same issue. I'm currently working around this by completely dereferencing the specification through swagger-cli:
swagger-cli.js bundle --outfile "${OPENAPI_SPEC_DEREF}" --dereference --type yaml "${OPENAPI_SPEC}"
Same problem here. Using @wolkenschieber solution solve partially the problem.
Using bundle command of redocly does not work at all :cry:
@wolkenschieber solution worked for me, but with this tweak:
swagger-cli bundle --outfile "${OPENAPI_SPEC_THAT_WILL_BE_USED_EVERYWHERE}" --dereference --type yaml "${OPENAPI_SPEC_THAT_NEEDS_TO_BE_DEREFERENCED}"
Does someone have an idea of what to do to resolve this issue?
how about this bug? what time will fix this bug?
Have the same issue with schema defined like
openapi: 3.0.0
info:
contact:
email: support_email
name: Support team
url: 'support_url'
description: DESC
title: mytitle
version: 1.0.0
components:
schemas:
$ref: 'components/schemas.yml'
...
and components/schemas file:
...
camera_response:
allOf:
- $ref: ./schemas/cameras/ApiCameraSchema.yml
...
receiving this error
java.lang.RuntimeException: Unable to load RELATIVE ref: ./schemas/cameras/ApiCameraSchema.yml path: my_openapi_project_dir
at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:243)
at io.swagger.v3.parser.ResolverCache.loadRef(ResolverCache.java:149)
at io.swagger.v3.parser.processors.ExternalRefProcessor.processRefToExternalSchema(ExternalRefProcessor.java:89)
at io.swagger.v3.parser.processors.SchemaProcessor.processReferenceSchema(SchemaProcessor.java:213)
at io.swagger.v3.parser.processors.SchemaProcessor.processComposedSchema(SchemaProcessor.java:141)
at io.swagger.v3.parser.processors.SchemaProcessor.processSchemaType(SchemaProcessor.java:50)
at io.swagger.v3.parser.processors.SchemaProcessor.processArraySchema(SchemaProcessor.java:199)
at io.swagger.v3.parser.processors.SchemaProcessor.processSchemaType(SchemaProcessor.java:47)
at io.swagger.v3.parser.processors.SchemaProcessor.processSchema(SchemaProcessor.java:39)
at io.swagger.v3.parser.processors.ComponentsProcessor.processSchemas(ComponentsProcessor.java:226)
at io.swagger.v3.parser.processors.ComponentsProcessor.processComponents(ComponentsProcessor.java:140)
at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:73)
at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:59)
at io.swagger.v3.parser.OpenAPIV3Parser.resolve(OpenAPIV3Parser.java:206)
at io.swagger.v3.parser.OpenAPIV3Parser.readContents(OpenAPIV3Parser.java:175)
at io.swagger.v3.parser.OpenAPIV3Parser.readLocation(OpenAPIV3Parser.java:91)
at io.swagger.parser.OpenAPIParser.readLocation(OpenAPIParser.java:16)
at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:541)
at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:599)
at org.openapitools.codegen.cmd.Generate.execute(Generate.java:441)
at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.RuntimeException: Could not find ./schemas/cameras/ApiCameraSchema.yml on the classpath
at io.swagger.v3.parser.util.ClasspathHelper.loadFileFromClasspath(ClasspathHelper.java:31)
at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:237)
... 21 common frames omitted
I just submitted an issue on the OpenAPI specification in the hope that they could make it clear in the next version that the syntax is correct. In the meantime, it could be fixed in https://github.com/swagger-api/swagger-parser/blob/065fa61ee74d42cbe62ebd53d1c7fedbfb6edf1e/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java#L823 Assuming this is the library used by openapi-generator
Any update here?
Saw the same issue any updates here?