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

Fix malli swagger defs #863

Merged
merged 10 commits into from
Mar 17, 2023
Merged

Conversation

cap10morgan
Copy link
Contributor

These are changes I needed to make in here to fix metosin/reitit#558 (I will be issuing a PR for the changes needed in reitit soon too).

This allows us to see the whole spec at once and fix a bug w/ custom registry definitions
...in json-schema. Also removes the leading `:` which the definitions keys also didn't have. This allows the refs and definitions keys to match and be standards-compliant.
Perhaps this really needs to be fixed at a lower level, but I noticed that at times I was getting definitions whose values where just refs back to the same definition. These seemed to come in addition to the correct result that had the actual definition in it. So this just filters out the circular ones.
@cap10morgan
Copy link
Contributor Author

Note that if you use any recursive schemas, you'll still see some missing defs in your swagger due to #464.

src/malli/json_schema.cljc Show resolved Hide resolved
src/malli/swagger.cljc Outdated Show resolved Hide resolved
src/malli/swagger.cljc Show resolved Hide resolved
src/malli/json_schema.cljc Show resolved Hide resolved
src/malli/swagger.cljc Show resolved Hide resolved
src/malli/swagger.cljc Show resolved Hide resolved
src/malli/swagger.cljc Outdated Show resolved Hide resolved
src/malli/swagger.cljc Show resolved Hide resolved
src/malli/swagger.cljc Show resolved Hide resolved
([x]
(swagger-spec x nil))
([x options]
(expand-qualified-keywords x options)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see some tests for swagger-spec

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll see what I can put together from existing spec / schema swagger-spec tests and whatever was in reitit-malli for its roughly-equivalent code.

@opqdonut
Copy link
Member

opqdonut commented Mar 8, 2023

Good stuff! Moving the malli swagger generation from reitit to malli would be consistent with what we do with spec & schema.

@cap10morgan
Copy link
Contributor Author

@opqdonut Is this just waiting on some tests for swagger-spec now?

@opqdonut
Copy link
Member

Yeah, just waiting for tests.

See my comment for one possible test case.

...in json-schema-test/references-test. Fix was in 23488b2.
...including one commented-out failing test that should pass once issue metosin#464 is fixed.
@cap10morgan
Copy link
Contributor Author

OK @opqdonut I think this is ready for re-review. I even added a (commented-out) failing test for #464.

test/malli/swagger_test.cljc Show resolved Hide resolved
:name "body",
:required true,
:schema {:$ref "#/definitions/malli.swagger-test~1body",
:definitions {::body {:minLength 1, :type "string"}}}}]}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be :definitions {"malli.swagger-test~1body" ...}?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. I think it doesn't need to be because by the time it gets serialized to JSON it gets converted to that. The one aspect I'm a little surprised by is that the / -> ~1 conversion is happening. But maybe that's just because it's being converted by JSON-Schema-aware code? Probably worth a little more investigation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it would be good to know where that munging is happening

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jsonista and other clojure json libraries just convert :foo.bar/baz to "foo.bar/baz". Grepping for ~ doesn't give me any hits from the malli or reitit repositories.

Copy link
Contributor Author

@cap10morgan cap10morgan Mar 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! I remember now. We don't want those to have / changed to ~1 because they aren't pointers, they're just regular old JSON object keys. You have to encode / as ~1 only in JSON pointers (a.k.a. the values of $ref keys). See here. This is because the JSON keys in the pointers are separated by /, so if you have a / in a key (like we do here), you have to encode that as ~1 and it will then match your /-containing key.

So yeah, the qualified keywords get expanded into the appropriate string when serialized to JSON.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right. That makes sense. Could you add that link as a comment to -ref? I think this is ready merge after that!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I'll just do that for you so we can avoid the round-trip.

Copy link
Member

@opqdonut opqdonut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add the links I requested

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

Successfully merging this pull request may close these issues.

Malli -> Swagger transformation fails with custom registries
3 participants