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

[BUG] Using $ref under components/schema results in spec validation failure

Open dmitri-d opened this issue 4 years ago • 8 comments

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.

dmitri-d avatar Dec 17 '21 21:12 dmitri-d

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}"

wolkenschieber avatar Dec 18 '21 19:12 wolkenschieber

Same problem here. Using @wolkenschieber solution solve partially the problem.

Using bundle command of redocly does not work at all :cry:

ShauniArima avatar Jan 04 '22 15:01 ShauniArima

@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}"

greglearns avatar Jan 05 '22 21:01 greglearns

Does someone have an idea of what to do to resolve this issue?

ShauniArima avatar Jan 06 '22 16:01 ShauniArima

how about this bug? what time will fix this bug?

zhuxubin01 avatar Feb 08 '22 04:02 zhuxubin01

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

Dimon70007 avatar May 25 '22 14:05 Dimon70007

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

lasconic avatar Jul 18 '22 19:07 lasconic

Any update here?

pontus-eliason avatar Feb 08 '24 13:02 pontus-eliason

Saw the same issue any updates here?

gaurao29 avatar Aug 15 '24 16:08 gaurao29