Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dmitri-d opened this issue Dec 17, 2021 · 10 comments
Open

Comments

@dmitri-d
Copy link

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.

@wolkenschieber
Copy link

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

@ShauniArima
Copy link

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

Using bundle command of redocly does not work at all 😢

@greglearns
Copy link

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

@ShauniArima
Copy link

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

@zhuxubin01
Copy link

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

@Dimon70007
Copy link

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

@lasconic
Copy link

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

@pontus-eliason
Copy link

Any update here?

@gaurao29
Copy link

Saw the same issue any updates here?

@rloreal
Copy link

rloreal commented Sep 12, 2024

any update ? bundling of my api results is a 174 Mb file, not processable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants