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

[Go] Generate XML struct annotations #490

Closed
langston-barrett opened this issue Jul 7, 2018 · 5 comments
Closed

[Go] Generate XML struct annotations #490

langston-barrett opened this issue Jul 7, 2018 · 5 comments

Comments

@langston-barrett
Copy link

Description

When generating Go code, one gets types tagged with JSON:

type Foo struct {
	Str string `json:"Str,omitempty"`
}

This is awesome for JSON APIs. However, OpenAPI also allows for application/xml content-types! I am writing an XML API and currently have to manually go in and tag these types.

openapi-generator version

The Docker CLI generator.

Command line used for generation
sudo docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
       -i /local/swagger.yml \
       -g go \
       -o /local/generated/client
Steps to reproduce

Write any swagger.yml and generate Go code

Related issues/PRs
Suggest a fix/enhancement

Automatically annotate all structs with both XML and JSON names, to start. The tags are space separated.

@langston-barrett
Copy link
Author

Is this done with -D withXml=true? It's hard to figure out the command line options when the software crashes when called with --help.

@grokify
Copy link
Member

grokify commented Jul 7, 2018

Yes, -D withXml=true will produce the XML struct tags. You can use it with other flags like -D packageName=petstore,withXml=true.

It could make some sense to include both XML and JSON by default if XML were more popular, but the vast majority of APIs I encounter these days don't use XML. Is it different for you?

Perhaps we can focus on making help easier to access? For example, we could look into aliasing --help / -h to help or minimally provide a nice message to use help instead of throwing an error? Generically making help easier to access can have multiple benefits, like choosing the packageName.

I use the following:

help

$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar help
usage: openapi-generator-cli <command> [<args>]

The most commonly used openapi-generator-cli commands are:
    config-help   Config help for chosen lang
    generate      Generate code with the specified generator.
    help          Display help information
    langs         Shows available languages (deprecated. use 'list' instead)
    list          Lists the available generators
    meta          MetaGenerator. Generator for creating a new template set and configuration for Codegen.  The output will be based on the language you specify, and includes default templates to include.
    validate      Validate specification
    version       Show version information

See 'openapi-generator-cli help <command>' for more information on a specific
command.

config-help -l go

$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar config-help -l go

CONFIG OPTIONS
	packageName
	    Go package name (convention: lowercase). (Default: openapi)

	hideGenerationTimestamp
	    Hides the generation timestamp when files are generated. (Default: true)

	packageVersion
	    Go package version. (Default: 1.0.0)

	withXml
	    whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML) (Default: false)

	prependFormOrBodyParameters
	    Add form or body parameters to the beginning of the parameter list. (Default: false)

@grokify
Copy link
Member

grokify commented Jul 7, 2018

It's hard to figure out the command line options when the software crashes when called with --help.

Good point. I updated the error message here: #494

@langston-barrett
Copy link
Author

@grokify Thanks for the quick response! I must be putting this option in the wrong place, because I'm still not seeing any XML tags. Does this look right?

sudo docker run --rm -v ${PWD}:/local \
     openapitools/openapi-generator-cli generate \
     -i /local/swagger.yml \
     -g go \
     -D withXML=true \
     -o /local/generated/client

sudo docker run --rm -v ${PWD}:/local \
     openapitools/openapi-generator-cli generate \
     -i /local/swagger.yml \
     -g go \
     -D withXML=true \
     -o /local/generated/server

@langston-barrett
Copy link
Author

Ah, I see that I was writing withXML instead of withXml. Perhaps an error should be thrown on unknown key-value arguments as well.

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