Skip to content

Commit 34ba0c0

Browse files
authored
Make schema-profunctor schema names derived and avoid name clashes between scopes. (#5151)
* Script for generating kotlin and typescript code from swagger.json. * openapi3 aka swagger: Make schema names unique(r). With this change, schema names contain a hash of the fully qualified name of the haskell type. This should avoid collisions in the hash table keeping track of all the schema references in openapi3. * Remove now unused name arguemnts from object, enum. * Keep named variants of object, enum, ... around for corner cases. * Versioned schemas. * Drive-by test coverage extension.
1 parent be8a397 commit 34ba0c0

130 files changed

Lines changed: 967 additions & 659 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

changelog.d/4-docs/swagger-hacking

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make schema-profunctor schema names derived and avoid name clashes between scopes.

hack/bin/generate-clients.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Simple OpenAPI client generator using openapi-generator
5+
# Usage: ./generate-clients.sh <swagger-url>
6+
7+
SWAGGER_URL="${1:-https://staging-nginz-https.zinfra.io/v16/api/swagger.json}"
8+
OUTPUT_DIR="$(pwd)/generated"
9+
10+
echo "==> Generating clients from: $SWAGGER_URL"
11+
echo "==> Output directory: $OUTPUT_DIR"
12+
13+
# Clean up previous runs
14+
rm -rf "$OUTPUT_DIR"
15+
mkdir -p "$OUTPUT_DIR"
16+
17+
# Download the spec
18+
echo "==> Downloading OpenAPI spec..."
19+
curl -s "$SWAGGER_URL" > "$OUTPUT_DIR/swagger.json"
20+
21+
# Check if docker is available
22+
if ! command -v docker &> /dev/null; then
23+
echo "Error: docker is not installed. Please install docker to use openapi-generator."
24+
echo "Alternative: install openapi-generator-cli via npm: npm install -g @openapitools/openapi-generator-cli"
25+
exit 1
26+
fi
27+
28+
# Generate TypeScript client
29+
echo ""
30+
echo "==> Generating TypeScript client..."
31+
docker run --rm \
32+
-v "$OUTPUT_DIR:/local" \
33+
openapitools/openapi-generator-cli:latest generate \
34+
-i /local/swagger.json \
35+
-g typescript-axios \
36+
-o /local/typescript \
37+
--additional-properties=supportsES6=true,npmName=wire-api-client,npmVersion=1.0.0
38+
39+
# Generate Kotlin client
40+
echo ""
41+
echo "==> Generating Kotlin client..."
42+
docker run --rm \
43+
-v "$OUTPUT_DIR:/local" \
44+
openapitools/openapi-generator-cli:latest generate \
45+
-i /local/swagger.json \
46+
-g kotlin \
47+
-o /local/kotlin \
48+
--additional-properties=packageName=com.wire.api.client,serializationLibrary=gson
49+
50+
echo ""
51+
echo "==> Done! Generated clients:"
52+
echo " TypeScript: $OUTPUT_DIR/typescript"
53+
echo " Kotlin: $OUTPUT_DIR/kotlin"

integration/test/Test/AccessUpdate.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import Testlib.Prelude
2929
import Testlib.ResourcePool
3030

3131
-- These two commented out tests exist to test the Setup.hs code.
32-
-- Both of these tests should not appear in the output.
32+
-- Neither of these tests should appear in the output.
3333

3434
-- testBar :: HasCallStack => App ()
3535
-- testBar = pure ()
@@ -89,6 +89,7 @@ testAccessUpdateGuestRemoved proto = do
8989

9090
bindResponse (getConversation alice conv) $ \res -> do
9191
res.status `shouldMatchInt` 200
92+
(length <$> (res.json %. "members.others" & asList)) `shouldMatchInt` 1
9293
res.json %. "members.others.0.qualified_id" `shouldMatch` objQidObject bob
9394

9495
-- @END

libs/saml2-web-sso/src/SAML2/WebSSO/Config.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ data ConfigRaw = ConfigRaw
8888

8989
instance ToSchema ConfigRaw where
9090
schema =
91-
object "ConfigRaw" $
91+
object $
9292
ConfigRaw
9393
<$> (_cfgRawLogLevel .= field "logLevel" schema)
9494
<*> (_cfgRawSPHost .= field "spHost" schema)
@@ -100,7 +100,7 @@ instance ToSchema ConfigRaw where
100100

101101
instance ToSchema MultiIngressDomainConfig where
102102
schema =
103-
object "MultiIngressDomainConfig" $
103+
object $
104104
MultiIngressDomainConfig
105105
<$> (_cfgSPAppURI .= field "spAppUri" schema)
106106
<*> (_cfgSPSsoURI .= field "spSsoUri" schema)

libs/saml2-web-sso/src/SAML2/WebSSO/Orphans.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ instance ToSchema URI where
6666
$ (parseURI strictURIParserOptions . Text.encodeUtf8) uriText
6767

6868
instance ToSchema Level where
69-
schema = assert exhaustive $ enum @Text "Level" $ mconcat $ el <$> [minBound ..]
69+
schema = assert exhaustive $ enum @Text $ mconcat $ el <$> [minBound ..]
7070
where
7171
el l = element (Text.pack (show l)) l
7272

libs/saml2-web-sso/src/SAML2/WebSSO/Types.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ import Data.Aeson
166166
import Data.Aeson.TH
167167
import Data.Bifunctor (first)
168168
import Data.CaseInsensitive qualified as CI
169+
import Data.Data (Typeable)
169170
import Data.List qualified as L
170171
import Data.List.NonEmpty (NonEmpty ((:|)))
171172
import Data.List.NonEmpty qualified as NL
@@ -271,7 +272,7 @@ data ContactPerson = ContactPerson
271272
-- (We may want to replace old template-haskell'ed ToJSON and FromJSON instances in hsaml2, but how?)
272273
instance Schema.ToSchema ContactPerson where
273274
schema =
274-
Schema.object "ContactPerson" $
275+
Schema.object $
275276
ContactPerson
276277
<$> (_cntType Schema..= Schema.field "type" Schema.schema)
277278
<*> (_cntCompany Schema..= Schema.maybe_ (Schema.optField "company" Schema.schema))
@@ -291,7 +292,7 @@ data ContactType
291292
-- (We may want to replace old template-haskell'ed ToJSON and FromJSON instances in hsaml2, but how?)
292293
instance Schema.ToSchema ContactType where
293294
schema =
294-
Schema.enum @ST.Text "ContactType" $
295+
Schema.enum @ST.Text $
295296
mconcat
296297
[ Schema.element "ContactTechnical" ContactTechnical,
297298
Schema.element "ContactSupport" ContactSupport,
@@ -312,7 +313,7 @@ data IdPMetadata = IdPMetadata
312313

313314
instance Schema.ToSchema IdPMetadata where
314315
schema =
315-
Schema.object "IdPMetadata" $
316+
Schema.object $
316317
IdPMetadata
317318
<$> (_edIssuer Schema..= Schema.field "issuer" Schema.schema)
318319
<*> (_edRequestURI Schema..= Schema.field "requestURI" Schema.schema)
@@ -346,9 +347,9 @@ data IdPConfig extra = IdPConfig
346347
deriving (Eq, Show, Generic)
347348
deriving (FromJSON, ToJSON, S.ToSchema) via (Schema.Schema (IdPConfig extra))
348349

349-
instance (Schema.ToSchema extra) => Schema.ToSchema (IdPConfig extra) where
350+
instance (Typeable (IdPConfig extra), Schema.ToSchema extra) => Schema.ToSchema (IdPConfig extra) where
350351
schema =
351-
Schema.object "IdPConfig" $
352+
Schema.object $
352353
IdPConfig
353354
<$> (_idpId Schema..= Schema.field "id" Schema.schema)
354355
<*> (_idpMetadata Schema..= Schema.field "metadata" Schema.schema)

libs/schema-profunctor/default.nix

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
, aeson
77
, aeson-qq
88
, base
9+
, base64-bytestring
910
, bifunctors
11+
, bytestring
1012
, comonad
1113
, containers
1214
, gitignoreSource
15+
, hashable
1316
, imports
1417
, insert-ordered-containers
1518
, lens
@@ -29,9 +32,12 @@ mkDerivation {
2932
libraryHaskellDepends = [
3033
aeson
3134
base
35+
base64-bytestring
3236
bifunctors
37+
bytestring
3338
comonad
3439
containers
40+
hashable
3541
imports
3642
lens
3743
openapi3

libs/schema-profunctor/schema-profunctor.cabal

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@ library
6262
-Wredundant-constraints -Wunused-packages
6363

6464
build-depends:
65-
aeson >=2.0.1.0
66-
, base >=4 && <5
65+
aeson >=2.0.1.0
66+
, base >=4 && <5
67+
, base64-bytestring
6768
, bifunctors
69+
, bytestring
6870
, comonad
6971
, containers
72+
, hashable
7073
, imports
7174
, lens
7275
, openapi3
@@ -83,6 +86,7 @@ test-suite schemas-tests
8386
other-modules:
8487
Paths_schema_profunctor
8588
Test.Data.Schema
89+
Test.Data.Schema.Names
8690

8791
hs-source-dirs: test/unit
8892
default-extensions:

0 commit comments

Comments
 (0)