OpenAPI-Specification icon indicating copy to clipboard operation
OpenAPI-Specification copied to clipboard

Extra JSON Reference properties ignored - reduces reusability of references

Open JimJafar opened this issue 10 years ago • 89 comments

Hi everyone.

I feel that the ignoring of extra properties when using JSON References goes against the goal of reusability.

Take this example from a fictional Swagger contract:

{
    addresses: [
        "homeAddress": {
            "description": "The premises at which electricity is provided.",
            "$ref": "#/definitions/Address"
        },
        "invoiceAddress": {
            "description": "The billing address - must be where the customer's credit card is registered.",
            "$ref": "#/definitions/Address"
        }
    ]
}

The "description" properties help to distinguish between the two uses of the "Address" Reference. Of course you could argue that better naming of the "homeAddress" and "invoiceAddress" properties could achieve this, but we are not always in control of property names in our data models and sometimes you need a more verbose description.

The Swagger editor (http://editor.swagger.io/#/) is an example of an application that could be improved by removing this limitation.

Is this something that has been raised previously? Please see here for a discussion on the Swagger Google Group: https://groups.google.com/forum/#!topic/swagger-swaggersocket/ewgimdO2cOI

JimJafar avatar Feb 08 '16 08:02 JimJafar

@hkosova, Thanks a lot! Didn't know the "allOf" could be used for simple fields as well.

sgeeroms avatar Jul 27 '18 11:07 sgeeroms

I ran into this issue because swagger 1.2 does support property descriptions where there is a reference to a different type:

{
  "properties": {
    "myProperty": {
      "type": "MyModel",
      "description": "Description of property"
    }
  }
}

In addition, swagger-converter (swagger 1.2 -> openapi 2.0 converter) generates the following from this definition:

{
  "properties": {
    "myProperty": {
      "$ref": "#/definitions/MyModel",
      "description": "Description of property"
    }
  }
}

This is not according to the openapi 2.0 specification. api-spec-converter gives a warning for the description field, as it resides next to $ref. Other tools often ignore the description.

bobvanderlinden avatar Feb 07 '19 08:02 bobvanderlinden

Hi My Project has user defined classes for Duration, Cost, etc and these are being used as nested fields in other schema classes and I need to give description to each nested fields which is referenced to that class and I am using swagger-annotation to generate docs. I tried the allOf but it didn't work.

E.g: class Duration{ int value; int text; }

@Schema class OfficeSchedule{ int val; String name; Duration startDuration; Duration endDuration;

public int getVal(){ return val; }

public String getName(){ return name; }

@Schema(description = "start duration of office schedule", accessMode = AccessMode.READ_ONLY) public Duration getStartDuration() { return startDuration; }

@Schema(description = "end duration of office schedule", accessMode = AccessMode.READ_ONLY) public Duration getEndDuration() { return endDuration; } }

I tried using allOf but Json stillis not giving referenced class property description and updating the same schema description and the last property with

Json Schema Output

"Duration": { "title": "Duration", "type": "object", "properties": { "double": { "type": "number", "format": "double" }, "bigDecimal": { "type": "number" } }, "description": "end duration of office schedule", "readOnly": true, "allOf": [ { "$ref": "#/components/schemas/Duration" } ] }

Above example is a sample and almost all classes declared for our project are being used internally in other classes as fields. Can anyone help with this I need to provide referenced property level descriptions?

ghost avatar Apr 29 '19 12:04 ghost

Hi, just adding one instance of reusaility need here.

My own personal need is to change the required fields of an object depending on which service it is requested in.

I tried to use the allOf trick but as I expected this is not well handled by the client generator. Anyway, I think this is not the purpose of the allOf element.

Arbuste avatar Aug 01 '19 09:08 Arbuste

I'd expect that this (OpenAPI 3)

"startLocation": {
    "description": "The location of the trip start.",
    "nullable": true,
    "oneOf": [
        {
            "$ref": "#/components/schemas/Location"
        }
    ]
},

or this (Swagger 2)

"startLocation": {
    "description": "The location of the trip start.",
    "allOf": [
        {
            "$ref": "#/components/schemas/Location"
        }
    ]
},

should be allowed, but eg. swagger-codegen does not allow this. Why is one of these samples not the current way to go? Or something similar to avoid $ref with additional properties?

RicoSuter avatar Sep 12 '19 15:09 RicoSuter

In Schema Objects, $ref siblings will be supported in OAS 3.1 which will use the JSON Schema 2019-09. As mentioned in JSON Schema 2019-09 release notes, it now allows keywords alongside $ref.

However, this change does not affect $refs outside of Schema Objects, such as $refs in parameters or responses.

Related: #2099

hkosova avatar Feb 24 '20 16:02 hkosova

As @hkosova mentions, siblings are now welcome in schema objects, and will likely be allowed in various other places around the OpenAPI spec.

philsturgeon avatar Jul 09 '20 16:07 philsturgeon

I think that a good solution here would be to split the definition of description. you could split into field description and class description.

thejeff77 avatar May 18 '23 13:05 thejeff77

This is slightly related to new config property and annotation as detailed in wiki

frantuma avatar Oct 08 '24 17:10 frantuma