swagger-api / swagger-api/swagger-parser
Problem reading vendor extensions applied of properties defined by an external reference
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 867
- Forks
- 560
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 7
Description
I noticed this problem a while ago. I did report it to the penapi-generator/issues/14327 . At the time it wasn't a big issue and I suspected it was a problem with the swagger parser. Recently, it has become something of a more important issue for us, so I looked into how to fix it. Fixing it is not too difficult, what's difficult is deciding on if it should be fixed, or not.
All the following will refer to version 2.1.14 when mentioning line numbers. I figure this is fixed, where the current development branch is not.
I will explain the problem in the context of the parser. Starting with the schema definition, where we deserialize this simple model description
test_message:
type: object
properties:
primitive:
type: integer
x-format: u32
x-maitred-index: 0
in_line:
type: object
x-maitred-index: 1
properties:
id:
description: Vehical registation ID.
type: string
x-maitred-index: 0
description: in_line description
ref_obj:
$ref: '#/ref_obj'
x-maitred-index: 2
description: ref_obj external description
ref_obj:
type: object
required:
- id
description: ref_obj internal description
properties:
id:
description: Vehical registation ID.
type: string
x-maitred-index: 0
When you are deserializing the parameters and you call io.swagger.v3.parser.util.OpenAPIDserializer.getSchema() an the node generated for the last parameter ref_obj, you send in the following JsonNode
{
"description":"ref_obj external description",
"x-maitred-index":2,
"$ref":"net_path.yaml#/net_path"
}
The getSchema() method recognizes this as a ref and on line 2760 enters an if statement to load the referenced object. All that's fine, but once that's done, on line 2799 the loaded schema is returned. This means that the description is ignored, which it probably should be, but also the vendor extension field is ignored. This I would argue is a a error. The vendor extension on this parameter are important, at least to us.
Among the other fields that are also ignored are:
- default
- nullable
These might also be important.
A quick hack fix is easy. Just replace line 2799 with
Map<String, Object> extensions = getExtensions(node);
if (extensions != null && extensions.size() > 0) {
schema.setExtensions(extensions);
}
return schema;
I am sure there is a much better solution, but this would fix our problem.
The big issue is that this change would have an effect on openapi-generator, and probably all other generators. Specifically, it would add to the vendor generated fields. In the case of openapi-generator it will only append extra fields, so in all probability no one would notice it, but there is no guarantee that other generators would not be effected more drastically.
These parameter fields on reference are a complex issue. Some of them can be ignored, but some of them have meanings that make sense, so I think they should be included.
So, is this a change that can be made, or not?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with io.swagger.v3.parser.util.OpenAPIDserializer.getSchema() around lines 2760-2799 in version 2.1.14 and inspect how a referenced schema node is loaded. Determine which reference-adjacent fields, including vendor extensions, default, and nullable, should be preserved, and verify the decision against the stated impact on openapi-generator and other generators.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100