Skip to content
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

Define part of a request schema with a raw json schema #554

Open
lud-wj opened this issue Jul 26, 2023 · 6 comments
Open

Define part of a request schema with a raw json schema #554

lud-wj opened this issue Jul 26, 2023 · 6 comments

Comments

@lud-wj
Copy link

lud-wj commented Jul 26, 2023

Hello,

For some route, I accept a body where one of the properties should comply to a JSON schema that is generated at compile time and will change over time. Is there a way to use a raw JSON string or a raw map as a schema in the operation call?

I have this error:

Must be one of:

- schema module, or schema struct
- list of schema modules, or schema structs
- boolean
- nil
- reference

Is there a way to achieve that?

Thanks!

@zorbash
Copy link
Contributor

zorbash commented Jul 26, 2023

Hi @lud-wj please see this example

An option is to do something like:

alias OpenApiSpex.{Operation, Reference}

@index_operation struct(Operation, Jason.decode!(Application.compile_env(:my_app, :index_operation)))
def index_operation() do
  %Operation{
    tags: ["users"],
    summary: "List users",
    description: "List all useres",
    operationId: "UserController.index",
    responses: %{
      200 => response("User List Response", "application/json", Schemas.UsersResponse),
      422 => %Reference{"$ref": "#/components/responses/unprocessable_entity"}
    }
  }
end

@lud-wj
Copy link
Author

lud-wj commented Jul 27, 2023

Hello, thanks but I do not understand, you do not use the @index_operation attribute below. Is it automaticaly registered or something like that?

@zorbash
Copy link
Contributor

zorbash commented Jul 27, 2023

@lud-wj The module attribute is not automatically registered. I put it there assuming you're already doing something similar for "should comply to a JSON schema that is generated at compile time".

@lud-wj
Copy link
Author

lud-wj commented Jul 27, 2023

I do something similar indeed, but that schema is only the request body schema.

I am not sure I can have a full operation as json, because the request body definition is a tuple:

request_body: {"User params", "application/json", SomeModule}

Sure, I can transform the json data to get an operation struct in the end, but there will not be a SomeModule, only a JSON schema. :request_body wants a OpenApiSpex.Schema though, and what I am looking for is a way to get just that from a raw JSON schema. Basically OpenApiSpex.Schema.from_json(Jason.decode!(json)). But I did not find from_json or anything alike.

I feel like :request_body should accept a valid JSON schema, not only an OpenApiSpex.Schema struct or module, don't you think?

@zorbash
Copy link
Contributor

zorbash commented Jul 27, 2023

I can't find a way to load a schema from JSON.

Have you considered having any such JSON schemas in a complete OpenAPI spec file which only defines schemas and load it using OpenApiSpex.OpenApi.from_map(Jason.decode(open_api_schema))?

Also might be worth looking what OperationBuilder and RequestBody to overcome the tuple obstacle.

@lud-wj
Copy link
Author

lud-wj commented Jul 27, 2023

Well we want to use the library as usual, except for this route, so I do not feel like having everything in a normalized map.

And I guess the builder will have the same error as above.

Nevermind, I'll have to make a raw-json-to-OpenApiSpex.Schema transformer. Should be quick. Might as well open a PR with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants