-
Hello, /~https://github.com/sphaso/api_spec_misunderstanding/blob/main/lib/apispecmisunderstanding_web/schema/abc.ex Contains two Schemas pointing to what's effectively the same reference (
Trying to follow the advice gives the same result (see "Also Not Working" test) However, using the Questions:
I'm happy to help clarifying this issue further if needed. Thank you for your time and attention. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @sphaso 👋 The short answer to your questions:
(Edited this comment to update the details on point 2) With more explaining: In this code: In this code: /~https://github.com/sphaso/api_spec_misunderstanding/blob/main/lib/apispecmisunderstanding_web/schema/abc.ex#L29 The third way is to reference the module name (not calling the
This test: /~https://github.com/sphaso/api_spec_misunderstanding/blob/main/test/apispecmisunderstanding_web/schema_test.exs#L10 can never work because the schema contains an explicit reference, but the calling code isn't passing in the other schemas for it to lookup. Try using This test: /~https://github.com/sphaso/api_spec_misunderstanding/blob/main/test/apispecmisunderstanding_web/schema_test.exs#L14 is working because the Submodule schema was in-lined. No schema lookups are required to cast the value. To help understand what's going on, you can run: {
"info": { "title": "Api Spex Misunderstanding API", "version": "1.0" },
"openapi": "3.0.0",
"security": [],
"servers": [{ "url": "http://localhost:4000", "variables": {} }],
"tags": [],
"components": {
"responses": {},
"schemas": {
"NotWorking": {
"items": { "anyOf": [{ "$ref": "#/components/schemas/Submodule" }] },
"title": "NotWorking",
"type": "array"
},
"Submodule": { "title": "Submodule", "type": "integer" },
"Working": {
"items": { "anyOf": [{ "title": "Submodule", "type": "integer" }] },
"title": "Working",
"type": "array"
}
}
},
"paths": {
"/api_spec/test": {
"post": {
"callbacks": {},
"description": "",
"operationId": "ApispecmisunderstandingWeb.TestController.create",
"parameters": [],
"requestBody": {
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/NotWorking" }
}
},
"description": "NotWorking",
"required": false
},
"responses": {},
"summary": "Plug works but cast_value does not?",
"tags": []
}
}
}
} |
Beta Was this translation helpful? Give feedback.
Hi @sphaso 👋
The short answer to your questions:
No, the reason it is not working is because the caller isn't passing enough information into
cast_value
for it to resolve the reference. I'm not sure why we haveOpenApiSpex.cast_value/2
, it will fail as soon as it encounters a%Reference{}
to another schema. Prefer to useOpenApiSpex.cast_value/3
which includes the OpenApi struct.The plug is working because it uses
OpenApiSpex.cast_and_validate/5
, including the fullOpenApi
struct, allowing references to be looked up in the components.(Edited this comment to update the details on point 2)
With more explaining:
In this code:
/~https://github.com/sphaso/api_spec_misunderstanding/blob/…