Home > database >  Proper validation of a referenced file when editing OpenAPI json in IntelliJ
Proper validation of a referenced file when editing OpenAPI json in IntelliJ

Time:02-10

I'm editing an OpenAPI JSON spec in IntelliJ. The automatic validation and code completion work very nicely.

The OpenAPI version used is 3.0.3, which IntelliJ detects correctly. It seems that it uses "openapi30.json" internally for validation, and all is good.

However, the file is getting very large and it's time to move some commonly-used models out of it using $ref.

This is where things break. The main spec looks like this (snippet):

{
  "openapi": "3.0.3",
  "info": {
    "title": "Cars REST API",
    "description": "Calls, Responses and DTOs for REST",
    "version": "1.0.0"
  },
  "components": {
    "schemas": {
      "car": {
        "$ref": "car.json"
      },
      "car-group": {
        "$ref": "car-group.json"
      }

And when editing it, IntelliJ recognizes it as "openapi30".

However, the referenced documents are not recognized. For example, the car.json file looks like this:

{
  "car": {
    "required": [
      "id",
      "name"
    ],
    "properties": {
      "id": {
        "type": "integer",
        "format": "int64"
      },
      "name": {
        "type": "string"
      },
      "tag": {
        "type": "string"
      }
    }
  }
}

And it's recognized simply as a JSON document, not an OpenAPI one, so there is no proper validation and no code completion, etc.

How does one tell IntelliJ that the file is part of an OpenAPI specification, to be validated as such? One should think this could be inferred from begin $ref'ed from the main spec, but this doesn't work.

Trying to add a $schema value in the referenced file had no effect (and probably isn't in line with the OpenAPI spec anyway).

Manually selecting the OpenAPI 3.0 file type for car.json is not helpful, because then validation (rightly) fails - as it doesn't have the top-level structure required (info, openapi, paths).

Perhaps some specific JSON schema mapping needs to be added in IntelliJ preferences? If that's the case, it would be actually a sub-schema or some tag in the main OpenAPI spec, how can that be done?

IntelliJ version is: IntelliJ IDEA 2021.3.2 (Ultimate Edition)

Any help would be greatly appreciated.

CodePudding user response:

Ron!

Such functionality is not yet supported. Please vote for https://youtrack.jetbrains.com/issue/IDEA-284305

  • Related