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

[haskell-servant][haskell-yesod] Use table-based conversion for field name conversion #16232

Merged
merged 2 commits into from
Aug 3, 2023

Conversation

msakai
Copy link
Contributor

@msakai msakai commented Aug 2, 2023

I noticed that haskell (haskell-servant) and haskell-yesod generators cannot handle field names starting with upper case characters. For example, the following spec contains the Message field, and the generated ToJSON/FromJSON instances of HelloResponse do not work.

openapi: 3.0.0
info:
  title: Test
  description: Test API
  version: 1.0.0
servers:
  - url: https://api.example.com/
    description: the server

paths:
  /hello:
    get:
      operationId: hello
      summary: "summary"
      description: "description."
      responses:
        "200":
          description: foo
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HelloResponse'

components:
  schemas:
    HelloResponse:
      type: object
      properties:
        id:
          type: string
          description: id
        Message:
          type: string
          description: text
      required:
        - id
        - Message
      description: description

For example, the following test program failed to convert JSON string into HelloResponse value.

module Main where

import qualified Data.Aeson as J
import qualified Data.ByteString.Lazy.Char8 as BL
import Test.Types

main :: IO ()
main = do
  let s = BL.pack "{\"Message\":\"bar\",\"id\":\"foo\"}"
  BL.putStrLn s
  let val :: HelloResponse
      Just val = J.decode s
  print val
  BL.putStrLn $ J.encode val
  print $ J.encode val == s

The problem is that those instances call the uncapitalize function even when the JSON field name starts with an upper-case letter.

uncapitalize :: String -> String
uncapitalize (first:rest) = Char.toLower first : rest
uncapitalize [] = []

To resolve this problem, this PR proposes to change the field name conversion to the one based on the conversion table.
(However, I'm curious if there is a better way or how other generators handle this issue.)

With the changes in this PR, generated code looks like this:

-- | description
data HelloResponse = HelloResponse
  { helloResponseId :: Text -- ^ id
  , helloResponseMessage :: Text -- ^ text
  } deriving (Show, Eq, Generic, Data)

instance FromJSON HelloResponse where
  parseJSON = genericParseJSON optionsHelloResponse
instance ToJSON HelloResponse where
  toJSON = genericToJSON optionsHelloResponse
instance ToSchema HelloResponse where
  declareNamedSchema = Swagger.genericDeclareNamedSchema
    $ Swagger.fromAesonOptions
    $ optionsHelloResponse

optionsHelloResponse :: Options
optionsHelloResponse =
  defaultOptions
    { omitNothingFields  = True
    , fieldLabelModifier = \s -> fromMaybe ("did not find JSON field name for " ++ show s) $ lookup s table
    }
  where
    table =
      [ ("helloResponseId", "id")
      , ("helloResponseMessage", "Message")
      ]

and, the above-mentioned test program succeeds to convert JSON string and HelloResponse value:

{"Message":"bar","id":"foo"}
HelloResponse {helloResponseId = "foo", helloResponseMessage = "bar"}
{"Message":"bar","id":"foo"}
True

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • In case you are adding a new generator, run the following additional script :
    ./bin/utils/ensure-up-to-date
    
    Commit all changed files.
  • File the PR against the correct branch: master (6.3.0) (minor release - breaking changes with fallbacks), 7.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

cc: @f-f @Drezil

… name conversion

Current fieldLabelModifier implementation always produces uncapitalize
name, but it is inappropriate if the original JSON field name begins
with a capital letter.
@wing328
Copy link
Member

wing328 commented Aug 2, 2023

@msakai thanks for the PR. Can you please PM me via https://join.slack.com/t/openapi-generator/shared_invite/zt-12jxxd7p2-XUeQM~4pzsU9x~eGLQqX2g when you've time?

@msakai
Copy link
Contributor Author

msakai commented Aug 2, 2023

@wing328 Sure!
PM = private message = direct message?

@wing328
Copy link
Member

wing328 commented Aug 2, 2023

yes please

@wing328
Copy link
Member

wing328 commented Aug 3, 2023

tested locally and the result is good:

  deleteUserByTextR
    returns 501 Not Implemented
  getUserByTextR
    returns 501 Not Implemented
  getUserLoginR
    returns 501 Not Implemented
  getUserLogoutR
    returns 501 Not Implemented
  putUserByTextR
    returns 501 Not Implemented

Finished in 0.0245 seconds
20 examples, 0 failures

open-api-petstore> Test suite open-api-petstore-test passed
Completed 2 action(s).

@wing328 wing328 merged commit 45d8027 into OpenAPITools:master Aug 3, 2023
11 checks passed
@wing328 wing328 added this to the 7.0.0 milestone Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants