-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
allow both readOnly and required for a given property #299
Comments
Hi, Thanks, I'm glad you like the software! Yes there was a misalignment here between OpenAPI and JSONSchema in the past. There has recently been a new version of OpenAPI that has removed David |
Ha I see. I wasn't aware of the alignment in 3.1. The alignment is a great feature that was needed but on the other hand, we've lost some useful features such as this one :/ I wish they wouldn't have drop features from OpenApi and instead add them to the JSON schema specification but I'm sure they had good reasons. I think Right now, I see two solutions :
{
"definitions": {
"common": {
"type": "object",
"properties": {
"id": {
"type": "number",
"readOnly": true
},
"title": {
"type": "string"
},
"post": {
"type": "string"
}
},
"required": ["title", "post"],
"additionalProperties": false
},
"input": {
"allOf": [
{"$ref": "#/definitions/common"},
{"properties": {"id": false}}
]
},
"output": {
"allOf": [
{"$ref": "#/definitions/common"},
{"required": ["id"]}
]
}
}
} I guess the 2nd solution is the most robust, since we don't tweak the schemas ourselves, but it's also much more verbose and this would create two SQLAlchemy models. What do you think ? |
Would the following slight modification for the second approach work? Define 2 schemas, the input schema that does not include the |
Hi, thanks for the suggestion ! Here's what I tried to do : components:
schemas:
EmployeeInput:
description: Person that works for a company.
type: object
x-tablename: employee
properties:
id:
type: integer
description: Unique identifier for the employee.
example: 0
x-primary-key: true
x-autoincrement: true
readOnly: true
name:
type: string
description: The name of the employee.
example: David Andersson
x-index: true
division:
type: string
description: The part of the company the employee works in.
example: Engineering
x-index: true
salary:
type: number
description: The amount of money the employee is paid.
example: 1000000.00
required:
- name
- division
EmployeeOutput:
allOf:
- $ref: "#/components/schemas/EmployeeInput"
- x-inherits: true
x-kwargs:
__mapper_args__:
required:
- id I'm not sure whether my code is incorrect, maybe I did something wrong. I'm having the following error :
In the end, since I have very limited time to work on my POC, here's what I did for now :
You can close this issue if you want. |
Closing for now. If you would like to fix the issue above, it is because each model needs to define at least 1 property itself. So if |
Hi! First off, thanks for sharing this lib, I think it's pretty cool :)
Summary
As the title says, I wish I'd be able to use both
readOnly
andrequired
for a given property. The goal is the following :readOnly
property (e.g. the tableid
)id
property asrequired
so that users know they will get this value in the response and so that I can validate the responses using Connexion.According to this issue, this feature is now part of the OpenAPI sepcification.
Setup
In order to test whether this library allowed this use case, I used your Connexion example and I slightly modified the
Employee
schema :Result
With the 1st modification, as expected, the swagger UI is not expecting the
id
property for the POST requests anymore. Great !However, we get the following error (full traceback) :
Basically, the JSON schema validator is complaining that the
id
property is missing while it's required.Finally, when we remove the
required
property (2nd modification), it works fine. However, the schema is not really accurate anymore from the response point of view.Question
Is there any way to get around this ?
The text was updated successfully, but these errors were encountered: